如何使用 C# 将字符串转换为 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10946888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:53:22 来源:igfitidea点击:
How to convert string to XML using C#
提问by Aravind Srinivas
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 文档......请指导我如何做到这一点
采纳答案by Waqar
Use LoadXml Method of XmlDocument;
使用 XmlDocument 的 LoadXml 方法;
string xml = "<head><body><Inner> welcome </head> </Inner> <Outer> Bye</Outer></body></head>";
xDoc.LoadXml(xml);
回答by Asif Mushtaq
xDoc.LoadXML("<head><body><Inner> welcome </head> </Inner> <Outer> Bye</Outer>
</body></head>");
回答by Csaba Benko
// 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.
我认为这应该有效。
回答by Carlos Maia de Morais
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"

