C# XDocument如何使用linq过滤元素
C# XDocument how to use linq to filter elements
我正在使用
从内存流中读取 xml
XDocument doc = XDocument.Load(targetStream);
foreach (XElement element in doc.Root.Elements())
{
Console.WriteLine(element);
}
我的元素之一如下所示。我不确定为什么将命名空间 xmlns 添加到每个元素。基本上我想过滤所有 'Header' 序列号 = 01
的元素
<Header xmlns="LR/2020-21">
<CollectionDetails>
<Collection>ILR</Collection>
<Year>2021</Year>
<FilePreparationDate>2020-05-08</FilePreparationDate>
</CollectionDetails>
<Source>
<ProtectiveMarking>OFFICIAL-SENSITIVE-Personal</ProtectiveMarking>
<UKPRN>99999999</UKPRN>
<SoftwareSupplier>SupplierName</SoftwareSupplier>
<SoftwarePackage>SystemName</SoftwarePackage>
<Release>1</Release>
<SerialNo>01</SerialNo>
<DateTime>2020-05-08T09:14:05</DateTime>
<!-- This and the next element only appear in files generated by FIS -->
<ReferenceData>Version5.0, LARS 2018-08-01</ReferenceData>
<ComponentSetVersion>1</ComponentSetVersion>
</Source>
</Header>
即使是下面的代码也会带来空记录
IEnumerable<XElement> list1 =
from el in doc.Descendants("Header")
where (string)el.Attribute("xmlns") == "LR/2020-21"
select el;
谁能帮我解决这个问题。
谢谢
因此,如果您想过滤名称空间,您需要为元素名称提供正确的名称空间。像这样:
var ns = XNamespace.Get("LR/2020-21");
var headers = doc.Root
.Elements(ns + "Header")
.Where(e => (string)e.Element(ns + "Source").Element(ns + "SerialNo") == "01");
如果在同一个 Header
元素中存在多个 SerialNo
您可以使用以下表达式。
var ns = XNamespace.Get("LR/2020-21")
var matchingHeaders = doc.Root
.Elements(ns + "Header")
.Where(header => header
.Elements(ns + "Source")
.Any(source => source
.Elements(ns + "SerialNo")
.Any(serialNo => (string)serialNo == "01")))
我正在使用
从内存流中读取 xmlXDocument doc = XDocument.Load(targetStream);
foreach (XElement element in doc.Root.Elements())
{
Console.WriteLine(element);
}
我的元素之一如下所示。我不确定为什么将命名空间 xmlns 添加到每个元素。基本上我想过滤所有 'Header' 序列号 = 01
的元素<Header xmlns="LR/2020-21">
<CollectionDetails>
<Collection>ILR</Collection>
<Year>2021</Year>
<FilePreparationDate>2020-05-08</FilePreparationDate>
</CollectionDetails>
<Source>
<ProtectiveMarking>OFFICIAL-SENSITIVE-Personal</ProtectiveMarking>
<UKPRN>99999999</UKPRN>
<SoftwareSupplier>SupplierName</SoftwareSupplier>
<SoftwarePackage>SystemName</SoftwarePackage>
<Release>1</Release>
<SerialNo>01</SerialNo>
<DateTime>2020-05-08T09:14:05</DateTime>
<!-- This and the next element only appear in files generated by FIS -->
<ReferenceData>Version5.0, LARS 2018-08-01</ReferenceData>
<ComponentSetVersion>1</ComponentSetVersion>
</Source>
</Header>
即使是下面的代码也会带来空记录
IEnumerable<XElement> list1 =
from el in doc.Descendants("Header")
where (string)el.Attribute("xmlns") == "LR/2020-21"
select el;
谁能帮我解决这个问题。
谢谢
因此,如果您想过滤名称空间,您需要为元素名称提供正确的名称空间。像这样:
var ns = XNamespace.Get("LR/2020-21");
var headers = doc.Root
.Elements(ns + "Header")
.Where(e => (string)e.Element(ns + "Source").Element(ns + "SerialNo") == "01");
如果在同一个 Header
元素中存在多个 SerialNo
您可以使用以下表达式。
var ns = XNamespace.Get("LR/2020-21")
var matchingHeaders = doc.Root
.Elements(ns + "Header")
.Where(header => header
.Elements(ns + "Source")
.Any(source => source
.Elements(ns + "SerialNo")
.Any(serialNo => (string)serialNo == "01")))