Код: Выделить всё
XmlDocument
Код: Выделить всё
XDocument
Пример:
Код: Выделить всё
static void Main(string[] args)
{
string message = "Hello, \x1EWorld!"; // string with control code 1E encoded.
// This block completes - create an xml document incorporating the message string
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("greeting");
xmlDoc.AppendChild(root);
root.InnerText = message;
Console.WriteLine(xmlDoc.OuterXml);
// Outputs: Hello, World!
// This block fails - XDocument creation of document containing control-code character x1E
try
{
XDocument xdoc = new XDocument(
new XElement("greeting", message)
);
Console.WriteLine(xdoc.ToString());
}
catch (Exception ex)
{
Console.WriteLine($"XDocument creation error: {ex}");
}
// This block completes - XmlDocument load document containing an  entity expression
string xmlWithEscapedHexEntity = xmlDoc.OuterXml; // Hello, World!";
xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlWithEscapedHexEntity);
Console.WriteLine(xmlDoc.OuterXml);
// This block fails - XDocument parse document containing an  entity expression
try
{
XDocument xDoc = XDocument.Parse(xmlWithEscapedHexEntity);
Console.WriteLine(xDoc.ToString());
}
catch (Exception ex)
{
Console.WriteLine($"XDocument parse failure: {ex}");
}
Console.ReadLine();
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... -character