Saving XML file contents to String or StringBuilder


I want to save all the contents of an XML file to a string or string file. Please let me know how I can achieve this?

My function should copy or save the contents of the XML file. Completely in a string or stringbuilder.
This is external content (XML file). After that I need to change the contents (onf field) of the xml file. Can I get it through C #. tell me.

I have the following content in XML format, I want to put it on one line and pass it to another function in order to complete my Work.


<wsa:Address xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address> <wsa:ReferenceParameters xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"> <wsman:ResourceURI>http://schema.unisys.com/wbem/wscim/1/cim- </wsa:ReferenceParameters> </p:Source> </p:INPUT>"; 

----------------------------------------------- ---

Respectfully,
Channels

+4
source share
3 answers
 using System.Xml.Linq; // load the file var xDocument = XDocument.Load(@"C:\MyFile.xml"); // convert the xml into string (did not get why do you want to do this) string xml = xDocument.ToString(); 

Now, using xDocument , you can manipulate XML and save it back -

  xDocument.Save(@"C:\MyFile.xml"); 
+5
source

Reading an XML file into a string is simple:

 string xml = File.ReadAllText(fileName); 

To access the content, you must read it in an XmlDocument:

 XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); 
+7
source

Why don't you read XML directly in XmlDocument

 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); 

When you need XML as a string, use the XmlNode.OuterXml property .

0
source

All Articles