54

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

4 Answers 4

97

Use LoadXml Method of XmlDocument;

string xml = "<head><body><Inner> welcome </head> </Inner> <Outer> Bye</Outer></body></head>";
xDoc.LoadXml(xml);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice and easy! Thxs
43
// 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.

2 Comments

Just as a note, you need to escape the quotes in order to compile that code, to escape them use double quotes (""). <person firstname=""Riley"" lastname=""Scott"" />
You're absolutely right. I forget about the double quotes inside the string. Corrected that.
21
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"

1 Comment

What if this test header has some special characters like < or >?
5
xDoc.LoadXML("<head><body><Inner> welcome </head> </Inner> <Outer> Bye</Outer>                    
                    </body></head>");

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.