简体   繁体   English

XML元素和命名空间

[英]XML Element and Namespace

I have the following method to parse XMLElements: 我有以下方法来解析XMLElements:

DisplayMessages(XElement root)
{
  var items = root.Descendants("Item");
  foreach (var item in items)
  {
     var name = item.Element("Name");
     ....
  }
}

In debug mode, I can see the root as XML like this: 在调试模式下,我可以看到如下所示的XML根:

<ItemInfoList>
  <ItemInfo>
    <Item>
      <a:Name>item 1</a:Name>
      ...
    <Item>
    ...

and var name is null (I expect to get "item 1"). 并且var名称为null(我希望得到“ item 1”)。 I tried to use "a:Name" but it caused exception("character : cannot be used in name"). 我尝试使用“ a:Name”,但它导致了异常(“ character:无法在名称中使用”)。 I am not sure if I have to set namespace in root XElelement or not. 我不确定是否必须在根XElelement中设置名称空间。 All the xml node under root should be in the same namespace. 根目录下的所有xml节点都应在同一命名空间中。

I am new to XElement. 我是XElement的新手。 In my codes, item.Element("Name") will get its children node "Name"'s value value, is that right? 在我的代码中,item.Element(“ Name”)将获得其子节点“ Name”的值,对吗?

You need to use element names that include namespace. 您需要使用包含名称空间的元素名称。 Try this: 尝试这个:

static void DisplayMessages(XElement root)
{
    var items = root.Descendants(root.GetDefaultNamespace() + "Item");
    foreach (var item in items)
    {
        var name = item.Element(item.GetNamespaceOfPrefix("a") + "Name");
        Console.WriteLine(name.Value);
    }
}

Note that operator + is overloaded for XNamespace class in order to make code shorter: XNamespace.Addition Operator . 请注意,XNamespace类的operator +被重载以使代码更短: XNamespace.Addition Operator

You do need to define the "a" namespace in the root element: 您确实需要在根元素中定义“ a”命名空间:

<Root a:xmlns="http:///someuri.com">
...
</Root>

Then you can select an element in a non-default namespace using this syntax in LINQ to XML: 然后,您可以使用LINQ to XML中的以下语法在非默认名称空间中选择一个元素:

XNamespace a = "http:///someuri.com"; // must match declaration in document
...
var name = item.Element(a + "Name");

EDIT: 编辑:

To retrieve the default namespace: 要检索默认名称空间:

XNamespace defaultNamespace = document.Root.GetDefaultNamespace();
// XNamespace.None is returned when default namespace is not explicitly declared

To find other namespace declarations: 要查找其他名称空间声明:

var declarations = root.Attributes().Where(a => a.IsNamespaceDeclaration);

Note that namespaces can be declared on any element though so you would need to recursively search all elements in a document to find all namespace declarations. 请注意,尽管可以在任何元素上声明名称空间,所以您将需要递归搜索文档中的所有元素以查找所有名称空间声明。 In practice though this is generally done in the root element, if you can control how the XML is generated then that won't be an issue. 实际上,尽管通常是在root元素中完成的,但是如果您可以控制XML的生成方式,那么这将不是问题。

You need to create XNames that have a non-null Namespace. 您需要创建具有非空命名空间的XName。 To do so, you have to create an XNamespace, and add the element name, see Creating an XName in a Namespace . 为此,您必须创建一个XNamespace并添加元素名称,请参阅在Namespace中创建XName

If you work with XML data that contains namespaces, you need to declare these namespaces. 如果使用包含名称空间的XML数据,则需要声明这些名称空间。 (That's a general observation I made, even though it seems to make it difficult to "just have a look" on data you don't know). (这是我所做的一般观察,尽管似乎很难“浏览”您不知道的数据)。

You need to declare an XNamespace for your XElements, as in these MSDN samples: Element() , XName 您需要为XElement声明一个XNamespace,如以下MSDN示例所示: Element()XName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM