Channa
Channa

Reputation: 83

Saving XML file content to String or StringBuilder


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

Upvotes: 5

Views: 21819

Answers (3)

Angshuman Agarwal
Angshuman Agarwal

Reputation: 4866

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");

Upvotes: 6

ABH
ABH

Reputation: 3439

Why don't you directly read XML to XmlDocument

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

When you need XML as string then use XmlNode.OuterXml property.

Upvotes: 0

Guffa
Guffa

Reputation: 700322

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);

Upvotes: 7

Related Questions