【发布时间】:2021-07-22 15:52:33
【问题描述】:
我正在使用从内存流中读取 xml
XDocument doc = XDocument.Load(targetStream);
foreach (XElement element in doc.Root.Elements())
{
Console.WriteLine(element);
}
我的元素之一如下所示。我不确定为什么将命名空间 xmlns 添加到每个元素。基本上我想过滤 SerialNo = 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;
谁能帮帮我。
谢谢
【问题讨论】:
-
你能显示根元素吗?是否设置了命名空间?
标签: c# linq-to-xml