C#使用XmlReader获取额外的空格值,但不能使用XmlDocument获取额外的空格值

时间:2017-08-30 13:04:28

标签: c# json xml json.net

我的情况我不太明白。阅读以下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>

使用以下代码:

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

我得到json,如下所示:

json1:

  

{&#34; XML&#34;:{&#34; @版本&#34;:&#34; 1.0&#34;&#34; @编码&#34;:&#34; UTF -8&#34;}&#34;根&#34; {&#34; @xmlns:的xsi&#34;:&#34; http://www.w3.org/2001/XMLSchema-instance&#34;&#34;国家和#34 ;:[{&#34;国家&#34; {&#34; COUNTRYCODE&#34;:&#34; CN&#34;&#34; CurrentStatus&#34;:&#34;主动&#34;} },{&#34;国家&#34;:[{&#34; COUNTRYCODE&#34;:&#34; AU&#34;&#34; CurrentStatus&#34;:&#34;取消&#34;} ,{&#34; COUNTRYCODE&#34;:&#34; CN&#34;&#34; CurrentStatus&#34;:&#34;取消&#34;},{&#34; COUNTRYCODE&#34;:& #34; JP&#34;&#34; CurrentStatus&#34;:&#34;取消&#34;},{&#34; COUNTRYCODE&#34;:&#34; SG&#34;&#34; CurrentStatus&#34;:&#34;取消&#34;},{&#34; COUNTRYCODE&#34;:&#34; US&#34;&#34; CurrentStatus&#34;:&#34;主动&#34 ;}]},{&#34; @xsi:无&#34;:&#34;真&#34;}]}}

json2:

  

{&#34;根&#34; {&#34; @xmlns:的xsi&#34;:&#34; http://www.w3.org/2001/XMLSchema-instance&#34;&#34;#空白&#34 ;: [&#34; \ n   &#34;,&#34; \ n&#34;,&#34; \ n&#34;,&#34; \ n&#34;],&#34;国家&#34;:[{& #34; #whitespace&#34;:[&#34; \ n&#34;,&#34; \ n   &#34;],&#34;国家&#34;:{&#34; #whitespace&#34;:[&#34; \ n&#34;,&#34; \ n&#34;,& #34; \ n
  &#34],&#34; COUNTRYCODE&#34;:&#34; CN&#34;&#34; CurrentStatus&#34;:&#34;主动&#34;}},{&#34;#空白&#34;:[&#34; \ n
  &#34;,&#34; \ n&#34;,&#34; \ n&#34;,&#34; \ n&#34;,&#34; \ n&#34;,& #34; \ n   &#34;],&#34;国家&#34;:[{&#34; #whitespace&#34;:[&#34; \ n&#34;,&#34; \ n&#34;, &#34; \ n
  &#34],&#34; COUNTRYCODE&#34;:&#34; AU&#34;&#34; CurrentStatus&#34;:&#34;取消&#34;},{&#34;#空白& #34;:[&#34; \ n   &#34;,&#34; \ n&#34;,&#34; \ n
  &#34],&#34; COUNTRYCODE&#34;:&#34; CN&#34;&#34; CurrentStatus&#34;:&#34;取消&#34;},{&#34;#空白& #34;:[&#34; \ n   &#34;,&#34; \ n&#34;,&#34; \ n
  &#34],&#34; COUNTRYCODE&#34;:&#34; JP&#34;&#34; CurrentStatus&#34;:&#34;取消&#34;},{&#34;#空白& #34;:[&#34; \ n   &#34;,&#34; \ n&#34;,&#34; \ n
  &#34],&#34; COUNTRYCODE&#34;:&#34; SG&#34;&#34; CurrentStatus&#34;:&#34;取消&#34;},{&#34;#空白& #34;:[&#34; \ n   &#34;,&#34; \ n&#34;,&#34; \ n
  &#34],&#34; COUNTRYCODE&#34;:&#34; US&#34;&#34; CurrentStatus&#34;:&#34;主动&#34;}]},{&#34; @xsi:无&#34;:&#34;真&#34;}]}}

为什么XmlReader会生成空格但XmlDocument却没有?考虑到XML值,我认为他们不应该在那里。

2 个答案:

答案 0 :(得分:1)

解决它:

settings.IgnoreWhitespace = true;

感谢@HenkHolterman和@finrod。

答案 1 :(得分:0)

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