Ogglas
Ogglas

Reputation: 69968

C# getting extra whitespace values with XmlReader but not with XmlDocument

I have a situation that I do not quite understand. When reading the following XML:

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Countries>
      <Country>
        <CountryCode>CN</CountryCode>
        <CurrentStatus>Active</CurrentStatus>
      </Country>
    </Countries>

    <Countries>
      <Country>
        <CountryCode>AU</CountryCode>
        <CurrentStatus>Cancelled</CurrentStatus>
      </Country>
      <Country>
        <CountryCode>CN</CountryCode>
        <CurrentStatus>Cancelled</CurrentStatus>
      </Country>
      <Country>
        <CountryCode>US</CountryCode>
        <CurrentStatus>Active</CurrentStatus>
      </Country>
    </Countries>

    <Countries xsi:nil="true" />
</Root>

With the following code:

//No whitespace
string xml = File.ReadAllText(fileInfo.FullName);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string json1 = JsonConvert.SerializeXmlNode(xmlDoc);

//With whitespace
XmlDocument doc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

using (XmlReader reader = XmlReader.Create(fileInfo.FullName, settings))
{
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element)
        {
            XmlNode node = doc.ReadNode(reader);
            string json2 = JsonConvert.SerializeXmlNode(node);
        }
    }
}

I get json that looks like this:

json1:

{"?xml":{"@version":"1.0","@encoding":"utf-8"},"Root":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","Countries":[{"Country":{"CountryCode":"CN","CurrentStatus":"Active"}},{"Country":[{"CountryCode":"AU","CurrentStatus":"Cancelled"},{"CountryCode":"CN","CurrentStatus":"Cancelled"},{"CountryCode":"JP","CurrentStatus":"Cancelled"},{"CountryCode":"SG","CurrentStatus":"Cancelled"},{"CountryCode":"US","CurrentStatus":"Active"}]},{"@xsi:nil":"true"}]}}

json2:

{"Root":{"@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","#whitespace":["\n ","\n ","\n ","\n"],"Countries":[{"#whitespace":["\n ","\n "],"Country":{"#whitespace":["\n ","\n ","\n
"],"CountryCode":"CN","CurrentStatus":"Active"}},{"#whitespace":["\n
","\n ","\n ","\n ","\n ","\n "],"Country":[{"#whitespace":["\n ","\n ","\n
"],"CountryCode":"AU","CurrentStatus":"Cancelled"},{"#whitespace":["\n ","\n ","\n
"],"CountryCode":"CN","CurrentStatus":"Cancelled"},{"#whitespace":["\n ","\n ","\n
"],"CountryCode":"JP","CurrentStatus":"Cancelled"},{"#whitespace":["\n ","\n ","\n
"],"CountryCode":"SG","CurrentStatus":"Cancelled"},{"#whitespace":["\n ","\n ","\n
"],"CountryCode":"US","CurrentStatus":"Active"}]},{"@xsi:nil":"true"}]}}

Why does XmlReader generate white space but XmlDocument does not? I don't think they should be there given the XML values.

Upvotes: 3

Views: 1484

Answers (2)

Mostafa Daneshfar
Mostafa Daneshfar

Reputation: 13

 XmlDocument doc = new XmlDocument();
        doc.PreserveWhitespace = false;
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ConformanceLevel = ConformanceLevel.Document;
        settings.IgnoreWhitespace = true;
        XmlReader reader = XmlReader.Create("XMLFile1.xml", settings);
        {

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element )
                {
                    XmlNode node = doc.ReadNode(reader);
                    string json2 = JsonConvert.SerializeXmlNode(node);
                    Console.WriteLine(json2.Trim());
                }
            }
        }

Upvotes: 0

Ogglas
Ogglas

Reputation: 69968

Solved it with:

settings.IgnoreWhitespace = true;

Thanks to @HenkHolterman and @finrod.

Upvotes: 1

Related Questions