【发布时间】:2020-01-31 11:30:06
【问题描述】:
如何使用 XDocument 找到以下结构的正确元素?
XML:
<Body xmlns="http://tempuri.org/body" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Address>
<Segment i:type="AddressSegment">
<Id>9999</Id>
</Segment>
</Address>
<Segmentation>
<Segment xmlns:d4p1="http://tempuri.org/foo" i:type="d4p1:FooSegment">
<d4p1:Id>63</d4p1:Id>
</Segment>
<Segment xmlns:d4p1="http://tempuri.org/bar" i:type="d4p1:BarSegment">
<d4p1:Id>159</d4p1:Id>
</Segment>
<Segment i:type="BasicSegment">
<Id>10</Id>
</Segment>
</Segmentation>
</Body>
C#:
using ConsoleApp1.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var xDoc = XDocument.Parse(Resources.XmlDoc);
var dataSegments = xDoc.Descendants(XName.Get("Segment", "http://tempuri.org/body")).ToList();
Console.ReadLine();
}
}
}
这将导致我的列表中的所有 Segment 元素。但是如果我只想选择 Foo 和 Bar 元素呢?如何查找 i:type 的元素?
【问题讨论】:
-
i的声明在哪里? -
@Innat3 已更新命名空间为“i”
标签: c# xml linq-to-xml