Home > Software design >  How can I preserve entity characters using Xml Document?
How can I preserve entity characters using Xml Document?

Time:01-17

My question is simple, but I just can't find why I have this problem and can't resolve it. I need to read a XML file with values and use them on Unity. For now on, I read my document with its path :

XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement root = doc.DocumentElement;

I have a Namespace Manager already configured. I read my data like this :

string text = node.SelectSingleNode("x:textRuns/x:DOMTextRun/x:characters", nsmgr).InnerText.Replace("
", Environment.NewLine);

My XML and the data I would like to extract :

<characters>Third occupant&#xD;folding seat</characters>

My objective is to replace this entity character : "& #xD;" with an Environment.NewLine.

I tried to :

  • Formalize the Xml in a file with a replace
  • Read with an InnerText, and an InnerXml
  • Make an entity char "detector"
  • Get the node with all its content (OuterXML)

It looks like this char, however you read it, is exclude and not readable, I just can't have it on my console.

CodePudding user response:

The entity has already been replaced once you extracted InnerText. Problem is, you have a CR (carriage return; 0x0D, \r) instead of a LF (line feed; 0x0A, \n). So replace "\r" by Environment.NewLine:

public static void Main() {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<characters>Third occupant&#xD;folding seat</characters>");
    string text = doc.SelectSingleNode("/characters").InnerText;
    text = text.Replace("\r", Environment.NewLine);
    Console.WriteLine(text);
}
  • Related