Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving XML file content to String or StringBuilder

Tags:

c#


I want to save The whole content of XML file to a string or stringbuilder. Please let me know how can i achive that??

My Function needs to copy or Save a XML file content Completely to string or stringbuilder.
It is External Content(XML File).After that I need to change the Content(onf field ) of the xml file Can i achive it through C#. please let me know.

I Have following content in XML format , i want to put into one string and pass that to another Function in order to achive 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>";

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

Regards,
Channaa

like image 888
Channa Avatar asked Jun 22 '12 11:06

Channa


2 Answers

Reading an XML file into a string is simple:

string xml = File.ReadAllText(fileName);

The to access the content, you would read it into an XmlDocument:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
like image 111
Guffa Avatar answered Oct 26 '22 03:10

Guffa


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 the XML & save it back -

           xDocument.Save(@"C:\MyFile.xml");
like image 24
Angshuman Agarwal Avatar answered Oct 26 '22 04:10

Angshuman Agarwal