XmlDocument不会在标记之间保存空格

时间:2022-06-20 21:44:49

Part of XML:

XML的一部分:

<text:p>text1 <text:span>text2</text:span> <text:span>text3</text:span>text4</text:p>

You can see a space between text:span tags with text2 and text3. When I call XmlDocument.Load method I have this picture:

您可以在text:span标记与text2和text3之间看到空格。当我调用XmlDocument.Load方法时,我有这张图片:

<text:p>text1 <text:span>text2</text:span><text:span>text3</text:span>text4</text:p>

White space was removed, but I need this space in this place. Set property "PreserveWhitespace = true" don't help

白色空间被移除了,但我在这个地方需要这个空间。设置属性“PreserveWhitespace = true”没有帮助

2 个解决方案

#1


5  

PreserveWhitespace works for me. My sample code is

PreserveWhitespace适合我。我的示例代码是

    string xml = "<root><p>text1 <span>text2</span> <span>text3</span>text4</p></root>";
    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.LoadXml(xml);

    Console.Write(doc.InnerXml);

This prints

<root><p>text1 <span>text2</span> <span>text3</span>text4</p></root>

with the space between the span elements still there.

仍然存在跨度元素之间的空间。

#2


1  

For those using XDocument instead of XmlDocument:

对于那些使用XDocument而不是XmlDocument的人:

XDocument.Load(xml, LoadOptions.PreserveWhitespace)

#1


5  

PreserveWhitespace works for me. My sample code is

PreserveWhitespace适合我。我的示例代码是

    string xml = "<root><p>text1 <span>text2</span> <span>text3</span>text4</p></root>";
    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.LoadXml(xml);

    Console.Write(doc.InnerXml);

This prints

<root><p>text1 <span>text2</span> <span>text3</span>text4</p></root>

with the space between the span elements still there.

仍然存在跨度元素之间的空间。

#2


1  

For those using XDocument instead of XmlDocument:

对于那些使用XDocument而不是XmlDocument的人:

XDocument.Load(xml, LoadOptions.PreserveWhitespace)