BaseXML is a library to manipulate and validate XML files. It evaluates XPath expressions, adds new nodes or attributes, and modifies existing ones. By design, BaseXML doesn't perform mutable operations on signed XML documents.
Also, BaseXML offers a set of validators to check the nodes of XML documents.
To use BaseXML, create a class inheriting from BaseDocument
to define mutable custom operations.
using BaseXml;
class Note: BaseDocument
{
public Note(string xml)
: base(xml)
{
}
public string Body
=> Evaluate(new XPath("/note/body"));
public void AddPS(string ps)
{
var psNode = $"<ps>{ps}</ps>";
AddSiblingNodeAfterFirstOf(psNode, new XPath("/note/body"));
}
}
BaseXML has methods to add or prepend child nodes, add sibling nodes before or after a given node, and change the value of existing nodes and attributes. These methods are:
- AddChildren
- PrependChildren
- AddSiblingNodeBeforeFirstOf
- AddSiblingNodeAfterFirstOf
- AddOrReplaceSiblingNodeAfterFirstOf
- ChangeValueOfNode
- ChangeValueOfAttribute
- AddOrChangeValueOfAttribute
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<note>
<from>Bob</from>
<to>Alice</to>
<subject>Subject</subject>
<body>A Body</body>
</note>";
var note = new Note(xml);
var body = note.Body;
// "A Body"
note.AddPS("I Love You");
var ps = note.PS;
// "I Love You"
Please, look at the Sample project to see how to add new nodes, evaluate an XPath and map a POCO to an XML node.
BaseXml provides some per-node validations on top of a FluentValidation validator.
using BaseXml;
using BaseXml.Validation;
string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<note>
<from>Bob</from>
<to>Alice</to>
<subject>Subject</subject>
<body>A Body</body>
</note>";
var note = new Note(xml);
var validator = new CheckDocument(new Dictionary<XPath, IEnumerable<IValidateNode>>
{
{ new XPath("/note/subject"), new[] { new Length(min: 1, max: 10) } },
{ new XPath("/note/body"), new[] { new Required() } }
});
ValidationResult results = validator.Validate(note);
results.IsValid
// true
These are BaseXML node validators:
- AreEqual
- InKeyValue
- Length
- Matches
- Required
- ValidateIf
- Value
Grab your own copy
Feel free to report any bug, ask for a new feature or just send a pull request. All contributions are welcome.
MIT