[英]XMLDocument to xml file
In my web services I am sending a XML document using this code, 在我的Web服务中,我使用此代码发送XML文档,
XmlDocument doc = new XmlDocument();
doc.LoadXml(myBigData.Serialize());
return result = doc.DocumentElement;
Now In my c# console app I am calling this web method using, 现在,在我的c#控制台应用程序中,我使用
XmlElement returnedDataFromWebMethod = myWbSercvices.WebMethod();
Now how can I convert this XML element to a xml file eg in my C drive so i can see if xml document as document, instead of going through it using foreach(XMLNode)
现在如何将这个XML元素转换为xml文件,例如在C驱动器中,这样我就可以查看xml文档是否为文档,而不是使用
foreach(XMLNode)
进行foreach(XMLNode)
create a new XmlDocument: 创建一个新的XmlDocument:
XmlDocument doc = new XmlDocument();
call your web method and save it in an XmlNode 调用您的Web方法并将其保存在XmlNode中
XmlNode returnedDataFromWebMethod = myWbSercvices.WebMethod();
append your element 追加你的元素
doc.AppendChild(returnedDataFromWebMethod);
save the document 保存文件
doc.Save("result.xml");
You can use XmlWriter and WriteTo method. 您可以使用XmlWriter和WriteTo方法。 http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.writeto.aspx
http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.writeto.aspx
Example: 例:
XmlWriterSettings xmlSetings = new XmlWriterSettings();
xmlSetings.Indent = true;
xmlSetings.Encoding = Encoding.ASCII;
XmlWriter writer = XmlWriter.Create(@"C:\someFile.xml", xmlSetings);
returnedDataFromWebMethod.WriteTo(writer);
You may try this: 您可以尝试以下方法:
var doc = new XmlDocument();
var node = doc.ImportNode(returnedDataFromWebMethod, true);
doc.AppendChild(node);
doc.Save("output.xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.