Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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.

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading