XMLDocument.Save增加了返回车厢到XML时,元素显示为空白 [英] XMLDocument.Save adds return carriages to XML when elements are blank

查看:236
本文介绍了XMLDocument.Save增加了返回车厢到XML时,元素显示为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载一个XML文档,有一些没有的innerText一些标记。

如果我填充一些数据,然后根据需要它的工作原理的innerText(你会看到打开的标签,innerText属性和结束标记都在同一行)像下面...

 <根>
  <组件>值小于/组件>
< /根>
 

问题出现的标记,没有价值。这些应该被如上述除粗的没有价值的显示以相同的方式,像下面...

 <根>
  &其中;组件>&所述; /组件>
< /根>
 

然而,当innerText属性有一个空字符串,它增加了一个回车符和放大器;换行未期望是什么!它结束了看起来像下面...

 <根>
  &其中;组件>
  < /组件>
< /根>
 

这是我目前的code能产生上述结果......

 的XmlDocument xmlDoc中=新的XmlDocument();
xmlDoc.Load(@C:\的test.xml);

//保存XML,然后清理
xmlDoc.Save(@C:\的test.xml);
 

解决方案

这固定它,我...

 的XmlDocument xmlDoc中=新的XmlDocument();
xmlDoc.Load(@C:\的test.xml);

//保存XML,然后清理
XmlWriterSettings设置=新XmlWriterSettings {缩进= TRUE};
XmlWriter的作家= XmlWriter.Create(@C:\的test.xml,设置);
xmlDoc.Save(作家);
 

I'm loading a XML Document that has some tags that have no innertext.

If I populate the innertext with some data then it works as needed (you get opening tag, innertext and closing tag all on one line) like the following...

<root>
  <element>value</element>
</root>

The problem arises with tags with no values. These SHOULD be displayed in the same way as above with the exception of no value of coarse, like the following...

<root>
  <element></element>
</root>

However, when the innertext has an empty string it adds a carriage return & line feed which is not what is expected! It ends up looking like the following...

<root>
  <element>
  </element>
</root>

This is my current code that yields the above results...

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\test.xml");

//Save the xml and then cleanup
xmlDoc.Save(@"C:\test.xml");

解决方案

This fixed it for me...

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\test.xml");

//Save the xml and then cleanup
XmlWriterSettings settings = new XmlWriterSettings { Indent = true };
XmlWriter writer = XmlWriter.Create(@"C:\test.xml", settings);
xmlDoc.Save(writer);

这篇关于XMLDocument.Save增加了返回车厢到XML时,元素显示为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆