.NET XDocument处理问题

时间:2018-08-28 12:49:50

标签: c# .net xml linq linq-to-xml

我具有以下XML,并且我试图获取等于974的“ MarcEntryInfo”元素值,没有人知道如何使用XDocument来表示它。

在我的C#代码中,我尝试获取该值,但似乎无法检索该值。

表达我的疑问的另一种方法是,MarcEntryInfo的子标记是否具有某个值的entryID,返回特定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>

2 个答案:

答案 0 :(得分:0)

为了简洁起见,我将XML截断了。注意事项:

  • 您需要声明名称空间才能找到您的元素
  • 您需要在XPath表达式中按名称空间引用元素
  • 您应该选择具有目标值的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>
}

答案 1 :(得分:0)

我认为您可以在此处使用Linq到XML

下面的代码将使您获得XML上的所有条目ID标记

如果需要特定值,可以添加.SingleOrDefault(el => el.Value ==“ 974”);

[TestMethod]
public void GetLongestCommonPrefixTest()
{
    var str1 = @"C:\dir\dir1\file1";
    var str2 = @"C:\dir\dir1\file2";
    var str3 = @"C:\dir\dir1\file3";
    var str4 = @"C:\dir\dir2\file3";
    var str5 = @"C:\dir\dir1\file1\file3";
    var str6 = @"C:\dir\dir1\file1\file3";


    var res = Utilities.GetLongestCommonPrefix(str1, str2, str3);

    Assert.AreEqual(@"C:\dir\dir1", res);

    var res2 = Utilities.GetLongestCommonPrefix(str1, str2, str3, str4);

    Assert.AreEqual(@"C:\dir", res2);

    var res3 = Utilities.GetLongestCommonPrefix(str1, str2, str3, str5);

    Assert.AreEqual(@"C:\dir\dir1", res3);

    var res4 = Utilities.GetLongestCommonPrefix(str5, str6);

    Assert.AreEqual(@"C:\dir\dir1\file1\file3", res4);

    var res5 = Utilities.GetLongestCommonPrefix(str5);

    Assert.AreEqual(str5, res5);

    var res6 = Utilities.GetLongestCommonPrefix();

    Assert.AreEqual(null, res6);

    var res7 = Utilities.GetLongestCommonPrefix(null);

    Assert.AreEqual(null, res7);
}
相关问题