【问题标题】:How to save Xdocument without root nodes如何在没有根节点的情况下保存 Xdocument
【发布时间】:2012-06-12 10:45:33
【问题描述】:

我有肥皂序列化 xml 文件以附加模式添加到文件中,这将导致多个像这样的根节点

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Image id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Spo.DataModel/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58">
<Name id="ref-4">Component</Name>
<ImmediateState xsi:type="a2:ModifiedState" xmlns:a2="http://schemas.microsoft.com/clr/nsassem/Spo.Plugins/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58">Current</ImmediateState>
</a1:Image>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Image id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Spo.DataModel/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58">
<Name id="ref-4">Connect</Name>
<ImmediateState xsi:type="a2:ModifiedState" xmlns:a2="http://schemas.microsoft.com/clr/nsassem/Spo.Plugins/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58">Current</ImmediateState>
</a1:Image>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我这样做是为了加载 XDocument

 StreamReader Sr = new StreamReader(filename);
 XDocument doc = XDocument.Parse("<rootnode>" + Sr.ReadToEnd() + "</rootnode>");

然后我对 doc 做了一些修改,最后我希望它在没有根节点的情况下保存,并且

<?xml version="1.0" encoding="utf-8"?> 

当我这样做时

doc.Save(filename);

我想在没有根节点的情况下保存它,因为我可以再次反序列化文件

所以请给我任何替代方法来实现....

提前致谢

【问题讨论】:

  • 首先 - 具有多个根节点的 XML 字符串无效。其次:你的 XML 一个根节点 - &lt;SOAP-ENV:Envelope&gt; - 那么你的问题是什么,真的吗?你打电话给doc.Save() 失败了吗?如果是这样:什么您收到的错误消息到底是什么?
  • 是的,我知道 xml 在没有根节点的情况下是无效的,但是当肥皂序列化多个对象时,我将有多个 根节点看到我的文件有两个 根节点和我的 doc.Save();不会失败,但我希望它在没有根节点的情况下保存,这样我就可以再次对它们进行反序列化,以任何替代方法在文件中保存没有根节点的修改后的 xdocumnet 文档?请帮忙
  • 当您需要再次反序列化 XML 时,为什么“人工”根节点会出现问题??
  • soapformatter.desirialize(filestream) 将通过异常,因为文件将不是soapforamtter格式
  • 基本上我希望我的输出文件没有根节点

标签: c# xml


【解决方案1】:

这将为您提供没有根节点的 xml。

StringBuilder sb = new StringBuilder();
doc.Root.Elements().ToList().ForEach(x => sb.Append(x.ToString()));
string xmlWithoutRootNodes = sb.ToString();
File.WriteAllText("file", xmlWithoutRootNodes);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2011-04-24
    • 1970-01-01
    • 2019-02-26
    • 2018-03-24
    相关资源
    最近更新 更多