alexeevyci
alexeevyci

Reputation: 281

Read XML file with XmlDocument in c#

I tried to read a XML file but I don't receive content of nodes that have just space or tab or new line. Please tell me where I'm wrong.

The XML file:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <paragraph>
        <sentence>
            <sequence>
                <word>aaa</word>
                <space> </space>
            </sequence>
            <sequence>
                <word>bbb</word>
                <space>    </space>
            </sequence>
            <sequence>
                <word>ccc</word>
                <space>?!</space>
            </sequence>
        </sentence>
    </paragraph>
</root>

The code:

XmlDocument doc = new XmlDocument();
doc.Load("D:/Licenta/files/struct.xml");
XmlNodeList sentences = doc.DocumentElement.SelectNodes("/root/paragraph");
foreach (XmlNode sentence in sentences) {
    Console.WriteLine(sentence.InnerText);
}
Console.ReadLine();

The output: aaabbbccc?!

Upvotes: 1

Views: 902

Answers (1)

spender
spender

Reputation: 120400

There's a property on XmlDocument called PreserveWhitespace which defaults to false causing the behaviour you're observing. You might want to consider switching it to true prior to loading your data into the document.

Upvotes: 2

Related Questions