.NET Standard library of XML related functionality.
ByteDev.Xml has been written as a .NET Standard 2.0 library, so you can consume it from a .NET Core or .NET Framework 4.6.1 (or greater) application.
ByteDev.Xml is hosted as a package on nuget.org. To install from the Package Manager Console in Visual Studio run:
Install-Package ByteDev.Xml
Further details can be found on the nuget page.
Releases follow semantic versioning.
Full details of the release notes can be viewed on GitHub.
Extension methods:
XDocument
- IsRootName
XElement
- GetChildElement
- GetChildElements
- GetChildElementValue
- GetChildElementValue
- GetAttributeValue
- GetAttributeValue
- HasDescendants
IEnumerable
- GetChildElement
- GetChildElements
String
- IsXml
- ContainsOnlyXmlChars
Example of serializing and deserializing:
[XmlRoot("product")]
public class ProductXml
{
[XmlElement("code")]
public string Code { get; set; }
[XmlElement("name")]
public string Name { get; set; }
}
// ...
var product = new ProductXml { Code = "code1", Name = "name1" };
IXmlDataSerializer serializer = new XmlDataSerializer();
var xml = serializer.Serialize(product, Encoding.UTF8);
var p = serializer.Deserialize<ProductXml>(xml);
Example of encoding a XML string:
var result = XmlEncoder.Encode("Using & special 'entity'");
// result == "Using & special 'entity'"
Example of removing illegal XML characters from a XML string:
char illegalChar = '\0';
string s = $"this {illegalChar} that {illegalChar} this";
var result = XmlSanitizer.Sanitize(s);
// result == "this that this"