我有以下XML文档
<Documents Databasename="Type1">
<Document>
<Documentnumber>xxxxxx</Documentnumber>
<Documentname>xxxxxx</Documentname>
<DocumentExtension>xxxxxx</DocumentExtension>
<ImportAction>xxxxxx</ImportAction>
<Authorname>xxxxxx</Authorname>
<Practicegroup>xxxxxx</Practicegroup>
<Createddate>xxxxxx</Createddate>
<Createdby>xxxxxx</Createdby>
<Modifieddate>xxxxxx</Modifieddate>
<Modifiedby>xxxxxx</Modifiedby>
<Lastaccessddate>xxxxxx</Lastaccessddate>
<Comments />
<Sector>Unknown</Sector>
<Source>Unknown</Source>
<Quality>Unknown</Quality>
<KnowHowContentType>Unknown</KnowHowContentType>
<ActualAutors />
<SubmittedBy />
<NumberofAccess>0</NumberofAccess>
<AreasofLaw />
</Document>
<Document>
<Documentnumber>xxxxxx</Documentnumber>
<Documentname>xxxxxx</Documentname>
<DocumentExtension>xxxxxx</DocumentExtension>
<ImportAction>xxxxxx</ImportAction>
<Authorname>xxxxxx</Authorname>
<Practicegroup>xxxxxx</Practicegroup>
<Createddate>xxxxxx</Createddate>
<Createdby>xxxxxx</Createdby>
<Modifieddate>xxxxxx</Modifieddate>
<Modifiedby>xxxxxx</Modifiedby>
<Lastaccessddate>xxxxxx</Lastaccessddate>
<Comments />
<Sector>Unknown</Sector>
<Source>Unknown</Source>
<Quality>Unknown</Quality>
<KnowHowContentType>Unknown</KnowHowContentType>
<ActualAutors />
<SubmittedBy />
<NumberofAccess>0</NumberofAccess>
<AreasofLaw />
</Document>
XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX 未知 未知 未知 未知 0 XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX 未知 未知 未知 未知 0
我想为给定属性Databasename选择DocumentNumber,DocumentExtension。如果我解释了自己,请告诉我。
答案 0 :(得分:1)
我没有太多使用Linq到XML,所以这可能不是最好的方法,但似乎有效:
string xml = @"
<root>
<Documents Databasename='Type1'>
<Document>
<Documentnumber>1234</Documentnumber>";
// etc..
XDocument xmlDoc = XDocument.Parse(xml);
var output = from item in xmlDoc.Descendants()
where item.Name == "Document"
&& item.Parent.Attribute("Databasename").Value == "Type1"
select new { Number = (string)item.Element("Documentnumber").Value,
Extension = item.Element("DocumentExtension").Value
};
foreach (var item in output) {
Console.WriteLine(item);
}
请注意,我在XML中添加了一个额外的“root”元素以使其工作(假设有多个“Documents”元素)。