5


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

2
  • 2
    What have you tried? Opening a file and getting all its contents is one of the most fundamental operations... Commented Jun 22, 2012 at 11:44
  • Why do you need to store entire XML file in a string ? Is the question like - Reading the XML file, changing element / attribute and saving it back ?. If yes, then there are enormous amount of samples available. Just search it on. Something like XDocument.Load(@"C:\MyFile.xml"); Commented Jun 22, 2012 at 11:55

3 Answers 3

7

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.