.NET XDocument 处理问题
.NET XDocument processing issue
我有以下 XML,我正在尝试获取等于 974 的 "MarcEntryInfo" 元素值,有谁知道这是如何使用 XDocument 表达的。
在我的 C# 代码中,我尝试获取该值,但它似乎无法检索该值。
另一种表达我所问内容的方式是 MarcEntryInfo 的子标签是否具有特定值的 entryID return 该特定 MarcEntryInfo 的文本子元素的字符串值。
谢谢
<LookupTitleInfoResponse xmlns="http://schemas.sirsidynix.com/symws/standard">
<TitleInfo>
<titleID>4971729</titleID>
<CallInfo>
<libraryID>PRODUCT</libraryID>
<classificationID>ALPHANUM</classificationID>
<callNumber>WS6689</callNumber>
<numberOfCopies>1</numberOfCopies>
<ItemInfo>
<itemID>4971729-1001</itemID>
<itemTypeID>TAPE</itemTypeID>
<currentLocationID>STORE</currentLocationID>
<homeLocationID>STORE</homeLocationID>
<chargeable>true</chargeable>
<fixedTimeBooking>false</fixedTimeBooking>
</ItemInfo>
</CallInfo>
<BibliographicInfo>
<MarcEntryInfo>
<label>MD-ARK</label>
<entryID>974</entryID>
<indicators></indicators>
<text>ark:/81055/vdc_100000006155.0x2afcee</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>L-ARK: INGESTED</label>
<entryID>975</entryID>
<indicators></indicators>
<text>ark:/81055/vdc_100055625567.0x000002</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>SHELFMARK</label>
<entryID>087</entryID>
<indicators></indicators>
<text>WS6689</text>
<entryTypeCodes>VR</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Unpublished series</label>
<entryID>441</entryID>
<indicators></indicators>
<text>Wildlife species reels</text>
<entryTypeCodes>MTR</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Other ref. no.</label>
<entryID>091</entryID>
<indicators></indicators>
<text>W Melanogrammus aeglefinus r1</text>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Product title</label>
<entryID>499</entryID>
<indicators></indicators>
<text>Melanogrammus aeglefinus r1</text>
<entryTypeCodes>MAR</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Format</label>
<entryID>310</entryID>
<indicators></indicators>
<text>1 tape reel</text>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Cataloguing status</label>
<entryID>971</entryID>
<indicators></indicators>
<text>prc</text>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Data source</label>
<entryID>976</entryID>
<indicators></indicators>
<text>WSF</text>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Content code</label>
<entryID>312</entryID>
<indicators></indicators>
<text>a</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
</BibliographicInfo>
</TitleInfo>
</LookupTitleInfoResponse>
为了简洁起见,我截断了 XML。需要注意的几点:
- 您需要声明命名空间才能找到您的元素
- 您需要在 XPath 表达式中按名称空间引用元素
- 您应该 select 具有目标值的
entryID
元素并导航到其父元素
示例代码如下。一旦你有了 entryInfo
元素,你就可以从它的子元素中提取你需要的任何信息。
void Main()
{
var xml = @"<LookupTitleInfoResponse xmlns=""http://schemas.sirsidynix.com/symws/standard"">
<TitleInfo>
<titleID>4971729</titleID>
<CallInfo>
<libraryID>PRODUCT</libraryID>
<classificationID>ALPHANUM</classificationID>
<callNumber>WS6689</callNumber>
<numberOfCopies>1</numberOfCopies>
<ItemInfo>
<itemID>4971729-1001</itemID>
<itemTypeID>TAPE</itemTypeID>
<currentLocationID>STORE</currentLocationID>
<homeLocationID>STORE</homeLocationID>
<chargeable>true</chargeable>
<fixedTimeBooking>false</fixedTimeBooking>
</ItemInfo>
</CallInfo>
<BibliographicInfo>
<MarcEntryInfo>
<label>MD-ARK</label>
<entryID>974</entryID>
<indicators></indicators>
<text>ark:/81055/vdc_100000006155.0x2afcee</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Content code</label>
<entryID>312</entryID>
<indicators></indicators>
<text>a</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
</BibliographicInfo>
</TitleInfo>
</LookupTitleInfoResponse>";
var nsManager = new XmlNamespaceManager(new NameTable());
nsManager.AddNamespace("ns", "http://schemas.sirsidynix.com/symws/standard");
var doc = XDocument.Parse(xml);
var entryInfo = doc.XPathSelectElement("//ns:entryID[contains(text(), '974')]/..", nsManager);
Console.WriteLine(entryInfo);
// Output:
// <MarcEntryInfo xmlns="http://schemas.sirsidynix.com/symws/standard">
// <label>MD-ARK</label>
// <entryID>974</entryID>
// <indicators></indicators>
// <text>ark:/81055/vdc_100000006155.0x2afcee</text>
// <entryTypeCodes>L</entryTypeCodes>
// </MarcEntryInfo>
}
我想你可以在这里使用 Linq XML
下面的代码可以让您获得 XML
上的所有条目 ID 标签
如果您需要特定值,可以添加 .SingleOrDefault(el=>el.Value=="974");
var xml = XElement.Load(YourDocumentPath);
var allEntryIds = xml.Elements()
.SingleOrDefault(el=>el.Name.LocalName=="TitleInfo")
.Elements()
.SingleOrDefault(el => el.Name.LocalName == "BibliographicInfo")
.Elements()
.Where(el => el.Name.LocalName == "MarcEntryInfo")
.Elements()
.Where(el => el.Name.LocalName == "entryID");
我有以下 XML,我正在尝试获取等于 974 的 "MarcEntryInfo" 元素值,有谁知道这是如何使用 XDocument 表达的。
在我的 C# 代码中,我尝试获取该值,但它似乎无法检索该值。
另一种表达我所问内容的方式是 MarcEntryInfo 的子标签是否具有特定值的 entryID return 该特定 MarcEntryInfo 的文本子元素的字符串值。
谢谢
<LookupTitleInfoResponse xmlns="http://schemas.sirsidynix.com/symws/standard">
<TitleInfo>
<titleID>4971729</titleID>
<CallInfo>
<libraryID>PRODUCT</libraryID>
<classificationID>ALPHANUM</classificationID>
<callNumber>WS6689</callNumber>
<numberOfCopies>1</numberOfCopies>
<ItemInfo>
<itemID>4971729-1001</itemID>
<itemTypeID>TAPE</itemTypeID>
<currentLocationID>STORE</currentLocationID>
<homeLocationID>STORE</homeLocationID>
<chargeable>true</chargeable>
<fixedTimeBooking>false</fixedTimeBooking>
</ItemInfo>
</CallInfo>
<BibliographicInfo>
<MarcEntryInfo>
<label>MD-ARK</label>
<entryID>974</entryID>
<indicators></indicators>
<text>ark:/81055/vdc_100000006155.0x2afcee</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>L-ARK: INGESTED</label>
<entryID>975</entryID>
<indicators></indicators>
<text>ark:/81055/vdc_100055625567.0x000002</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>SHELFMARK</label>
<entryID>087</entryID>
<indicators></indicators>
<text>WS6689</text>
<entryTypeCodes>VR</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Unpublished series</label>
<entryID>441</entryID>
<indicators></indicators>
<text>Wildlife species reels</text>
<entryTypeCodes>MTR</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Other ref. no.</label>
<entryID>091</entryID>
<indicators></indicators>
<text>W Melanogrammus aeglefinus r1</text>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Product title</label>
<entryID>499</entryID>
<indicators></indicators>
<text>Melanogrammus aeglefinus r1</text>
<entryTypeCodes>MAR</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Format</label>
<entryID>310</entryID>
<indicators></indicators>
<text>1 tape reel</text>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Cataloguing status</label>
<entryID>971</entryID>
<indicators></indicators>
<text>prc</text>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Data source</label>
<entryID>976</entryID>
<indicators></indicators>
<text>WSF</text>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Content code</label>
<entryID>312</entryID>
<indicators></indicators>
<text>a</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
</BibliographicInfo>
</TitleInfo>
</LookupTitleInfoResponse>
为了简洁起见,我截断了 XML。需要注意的几点:
- 您需要声明命名空间才能找到您的元素
- 您需要在 XPath 表达式中按名称空间引用元素
- 您应该 select 具有目标值的
entryID
元素并导航到其父元素
示例代码如下。一旦你有了 entryInfo
元素,你就可以从它的子元素中提取你需要的任何信息。
void Main()
{
var xml = @"<LookupTitleInfoResponse xmlns=""http://schemas.sirsidynix.com/symws/standard"">
<TitleInfo>
<titleID>4971729</titleID>
<CallInfo>
<libraryID>PRODUCT</libraryID>
<classificationID>ALPHANUM</classificationID>
<callNumber>WS6689</callNumber>
<numberOfCopies>1</numberOfCopies>
<ItemInfo>
<itemID>4971729-1001</itemID>
<itemTypeID>TAPE</itemTypeID>
<currentLocationID>STORE</currentLocationID>
<homeLocationID>STORE</homeLocationID>
<chargeable>true</chargeable>
<fixedTimeBooking>false</fixedTimeBooking>
</ItemInfo>
</CallInfo>
<BibliographicInfo>
<MarcEntryInfo>
<label>MD-ARK</label>
<entryID>974</entryID>
<indicators></indicators>
<text>ark:/81055/vdc_100000006155.0x2afcee</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
<MarcEntryInfo>
<label>Content code</label>
<entryID>312</entryID>
<indicators></indicators>
<text>a</text>
<entryTypeCodes>L</entryTypeCodes>
</MarcEntryInfo>
</BibliographicInfo>
</TitleInfo>
</LookupTitleInfoResponse>";
var nsManager = new XmlNamespaceManager(new NameTable());
nsManager.AddNamespace("ns", "http://schemas.sirsidynix.com/symws/standard");
var doc = XDocument.Parse(xml);
var entryInfo = doc.XPathSelectElement("//ns:entryID[contains(text(), '974')]/..", nsManager);
Console.WriteLine(entryInfo);
// Output:
// <MarcEntryInfo xmlns="http://schemas.sirsidynix.com/symws/standard">
// <label>MD-ARK</label>
// <entryID>974</entryID>
// <indicators></indicators>
// <text>ark:/81055/vdc_100000006155.0x2afcee</text>
// <entryTypeCodes>L</entryTypeCodes>
// </MarcEntryInfo>
}
我想你可以在这里使用 Linq XML
下面的代码可以让您获得 XML
上的所有条目 ID 标签如果您需要特定值,可以添加 .SingleOrDefault(el=>el.Value=="974");
var xml = XElement.Load(YourDocumentPath);
var allEntryIds = xml.Elements()
.SingleOrDefault(el=>el.Name.LocalName=="TitleInfo")
.Elements()
.SingleOrDefault(el => el.Name.LocalName == "BibliographicInfo")
.Elements()
.Where(el => el.Name.LocalName == "MarcEntryInfo")
.Elements()
.Where(el => el.Name.LocalName == "entryID");