Aravind Srinivas
Aravind Srinivas

Reputation: 643

How to convert string to XML using C#

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

Upvotes: 54

Views: 268878

Answers (4)

Carlos Maia de Morais
Carlos Maia de Morais

Reputation: 320

string test = "<body><head>test header</head></body>";

XmlDocument xmltest = new XmlDocument();
xmltest.LoadXml(test);

XmlNodeList elemlist = xmltest.GetElementsByTagName("head");

string result = elemlist[0].InnerXml; 

//result -> "test header"

Upvotes: 21

Csaba Benko
Csaba Benko

Reputation: 1161

// 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.

Upvotes: 43

Waqar
Waqar

Reputation: 2601

Use LoadXml Method of XmlDocument;

string xml = "<head><body><Inner> welcome </head> </Inner> <Outer> Bye</Outer></body></head>";
xDoc.LoadXml(xml);

Upvotes: 97

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

xDoc.LoadXML("<head><body><Inner> welcome </head> </Inner> <Outer> Bye</Outer>                    
                    </body></head>");

Upvotes: 5

Related Questions