简体   繁体   中英

How can I preserve entity characters using Xml Document?

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM