问题
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