我需要将 XML 字符串转换为 XmlElement [英] I need to convert an XML string into an XmlElement

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

问题描述

我正在寻找将包含有效 XML 的字符串转换为 C# 中的 XmlElement 对象的最简单方法.

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

你怎么能把它变成一个XmlElement?

How can you turn this into an XmlElement?

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

推荐答案

使用这个:

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

小心!!如果您需要先将此元素添加到另一个文档,则需要使用 ImportNode 导入它.

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

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

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