如何使用c#将字符串转换为XML

时间:2021-03-24 21:48:49

Global variable m_xDoc

全局变量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

现在我必须将这个字符串设置为XML文档……请指导我怎么做这件事

5 个解决方案

#1


49  

Use LoadXml Method of XmlDocument;

使用XmlDocument的LoadXml方法;

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

#2


16  

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

我认为这应该奏效。

#3


9  

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"

#4


4  

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

#5


-5  

THIS works for me.Thanks

这工作谢谢

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


49  

Use LoadXml Method of XmlDocument;

使用XmlDocument的LoadXml方法;

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

#2


16  

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

我认为这应该奏效。

#3


9  

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"

#4


4  

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

#5


-5  

THIS works for me.Thanks

这工作谢谢

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"