如何在没有根节点的情况下保存 Xdocument [英] How to save Xdocument without root nodes

查看:37
本文介绍了如何在没有根节点的情况下保存 Xdocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have soap serialized xml file in append mode to file which will result in multiple root nodes like this

<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

I have did like this to load XDocument

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

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

Then I did some modification to doc finally I want it save without root nodes and

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

当我这样做

doc.Save(filename);

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

I want to save it without root nodes because again I can deserialize the file

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

So please provide me any alternative way to achieve....

提前致谢

推荐答案

这会让你得到没有根节点的 xml.

This will get you the xml without the root nodes.

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

这篇关于如何在没有根节点的情况下保存 Xdocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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