c# - 如何从 xml 中检索属性

标签 c# .net xml c#-4.0

我在 XmlDocument 中有以下 xml。我正在尝试从中提取 className

<Registration className="Eng" type="DirectRegistration" state="Activated"     xmlns="http://xyz/Registration">
  <Fields>
    <Field type="abc" value="123456" />
    <Field type="xyz" value="789" />
  </Fields>
</Registration>

我试过下面的代码,但它没有给我 className

var xmlNode = xmlDoc.DocumentElement;

谁能帮我把 className 的值取出来。

非常感谢

最佳答案

你快到了:

var className = xmlDoc.DocumentElement.GetAttribute("className");

xmlDoc.DocumentElement 为您提供了 whole 元素; GetAttribute 从中提取一个单独的命名属性。

关于c# - 如何从 xml 中检索属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11950562/

相关文章:

c# - 如何使用 FK 设置集合属性?

c# - 使用 LDAP 查询从我公司的所有林中获取所有域

android - 当我在真实设备中打开 Activity 时应用程序崩溃

c# - Windows Phone 模拟器警告和配置

c# - Ninject:单例绑定(bind)语法?

C# 在另一个函数中打破其他函数循环

c# - 具有未知数字的动态 Linq 和替换值?

.net - 在 .net core 中使用旧的 .net 库

xml - 创建自定义 XML 映射

xml - xsl :template match attribute 中的正则表达式