我需要一个XML字符串转换成一个的XmlElement [英] I need to convert an XML string into an XmlElement

查看:565
本文介绍了我需要一个XML字符串转换成一个的XmlElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找最简单的方式包含有效XML到C#的的XmlElement 对象的字符串转换。



你怎么可以把它变成一个的XmlElement

 < ;项目><名称>扳手及LT; /名称>< /项目> 


解决方案

使用这样的:

 私有静态的XmlElement GetElement(字符串XML)
{
XmlDocument的DOC =新的XmlDocument();
doc.LoadXml(XML);
返回doc.DocumentElement;
}



当心!
如果你需要这个元素添加到另一个文件首先你需要导入它使用 ImportNode


I'm looking for the simplest way to convert a string containing valid XML into an XmlElement object in C#.

How can you turn this into an XmlElement?

<item><name>wrench</name></item>

解决方案

Use this:

private static XmlElement GetElement(string xml)
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    return doc.DocumentElement;
}

Beware!! If you need to add this element to another document first you need to Import it using ImportNode.

这篇关于我需要一个XML字符串转换成一个的XmlElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆