XMLDocument to xml file

谁都会走 提交于 2020-06-17 15:54:27

问题


In my web services I am sending a XML document using this code,

      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,

      XmlElement returnedDataFromWebMethod = myWbSercvices.WebMethod();

Now how can I convert this XML element to a xml file e.g. in my C drive so i can see if xml document as document, instead of going through it using foreach(XMLNode)


回答1:


You may try this:

var doc = new XmlDocument();
var node = doc.ImportNode(returnedDataFromWebMethod, true);
doc.AppendChild(node);
doc.Save("output.xml");



回答2:


create a new XmlDocument:

XmlDocument doc = new XmlDocument();

call your web method and save it in an XmlNode

XmlNode returnedDataFromWebMethod = myWbSercvices.WebMethod();

append your element

doc.AppendChild(returnedDataFromWebMethod);

save the document

doc.Save("result.xml");



回答3:


You can use XmlWriter and WriteTo method. 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);


来源:https://stackoverflow.com/questions/18204957/xmldocument-to-xml-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!