Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting string to XML

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
Is it possible to convert a string to XML? I have an XML document coming in to my web page through Request.QueryString["xml"] in string format. I need to create a new XML file made up of the XML in this string. Can anybody help me out?

Cheers,

lfc77
 
Instantiate a XmlDocument object, then call the LoadXml() method with your string. If it loads successfully (i.e. if it was valid XML), you can then call .Save() method to write it to a file.

Note that the XmlDocument class is only good for XML up to about a megabyte's worth.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
If your string doesn't contain the namespaces then you can use the following piece of code:
string xmlFrag =&quot;<book> &quot; +
&quot;<title>Pride And Prejudice</title>&quot; +
&quot;<genre>novel</genre>&quot; +
&quot;</book>&quot;;
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlFrag);
doc.Save(&quot;myfile.xml&quot;);
}
catch (Exception ex)
{
string sMsg = ex.GetType() + ex.Message;
throw new Exception (ex);
}
If your string contains namespaces then use XmlTextReader:
XmlParserContext context = new XmlParserContext(...);
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top