簡體   English   中英

如何使用XPathSelectElement查詢

[英]How to query with XPathSelectElement

我有以下類型的XML,其中包含大量的DocType值:

<InvalidDocTypes>
  <DocType>DocType1</DocType>
  <DocType>DocType2</DocType>
</InvalidDocTypes>

我正在嘗試使用以下查詢特定文檔類型的XML:

document.PriorDocumentType = "DocType1"
var node = doc.XPathSelectElement("//InvalidDocTypes[DocType='" + document.PriorDocumentType + "']");

我只期望XML中沒有值時,節點為null,但我總是得到null。 最好是對XML使用Linq查詢,或者XPathSelectElement怎么辦? 任何幫助,將不勝感激。 謝謝

我測試了您的代碼,它似乎可以正常工作-請在下面驗證控制台應用程序。 DocType存在時,它將打印整個InvalidDocTypes元素;在不存在時將顯示null:

using System;
using System.Xml.Linq;
using System.Xml.XPath;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            var xml = @"<InvalidDocTypes>
  <DocType>DocType1</DocType>
  <DocType>DocType2</DocType>
</InvalidDocTypes>";

            var documentType = "DocType1";

            var xmlDocument = XDocument.Parse(xml);
            var node = xmlDocument.XPathSelectElement("//InvalidDocTypes[DocType='" + documentType + "']");
            Console.WriteLine(node);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM