Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to XML using C#

Tags:

c#

xml

Global variable m_xDoc

I have a property of

public XmlDocument xDoc {     get {return m_xDoc; }     set {value = m_xDoc; }            }   string xml = "<head><body><Inner> welcome </head></Inner><Outer> Bye</Outer></body></head>" 

Now I have to set that property with this string as XML document ... please guide me how to do this

like image 891
Aravind Srinivas Avatar asked Jun 08 '12 10:06

Aravind Srinivas


People also ask

What is the difference between XDocument and XmlDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.


2 Answers

Use LoadXml Method of XmlDocument;

string xml = "<head><body><Inner> welcome </head> </Inner> <Outer> Bye</Outer></body></head>"; xDoc.LoadXml(xml); 
like image 183
Waqar Avatar answered Sep 22 '22 18:09

Waqar


// using System.Xml;  String rawXml =       @"<root>           <person firstname=""Riley"" lastname=""Scott"" />           <person firstname=""Thomas"" lastname=""Scott"" />       </root>";  XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(rawXml); 

I think this should work.

like image 29
Csaba Benko Avatar answered Sep 23 '22 18:09

Csaba Benko