Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XElement.XPathSelectElements不返回匹配的元素_C#_Xml_Xpath - Fatal编程技术网

C# XElement.XPathSelectElements不返回匹配的元素

C# XElement.XPathSelectElements不返回匹配的元素,c#,xml,xpath,C#,Xml,Xpath,让System.Xml.XPath中的扩展方法为我工作时遇到了一些问题。我使用的是.NET4.5和VS2012 我在下面提供了一个基本示例,说明了我要做的事情(即使用扩展方法来使用XPath选择XML节点)。使用检查预期结果的健全性 对于每种情况,我都返回0个元素。我做错了什么 class Program { static void Main(string[] args) { XElement root = XElement.Parse("<a><

System.Xml.XPath
中的扩展方法为我工作时遇到了一些问题。我使用的是.NET4.5和VS2012

我在下面提供了一个基本示例,说明了我要做的事情(即使用扩展方法来使用XPath选择XML节点)。使用检查预期结果的健全性

对于每种情况,我都返回0个元素。我做错了什么

class Program
{
    static void Main(string[] args)
    {
        XElement root = XElement.Parse("<a><b /><b /><b><c id='1'/><c id='2' /></b></a>");
        ReportMatchingNodeCount(root, "/a"); // I would expect 1 match
        ReportMatchingNodeCount(root, "/a/b"); // I would expect 3 matches
        ReportMatchingNodeCount(root, "/a/b/c"); // I would expect 2 matches
        ReportMatchingNodeCount(root, "/a/b/c[@id='1']"); // I would expect 1 match
        ReportMatchingNodeCount(root, "/a/b/c[@id='2']"); // I would expect 1 match
        Console.ReadLine();
    }

    private static void ReportMatchingNodeCount(XElement root, string xpath)
    {
        int matches = root.XPathSelectElements(xpath).Count();
        Console.WriteLine(matches);
    }
}
类程序
{
静态void Main(字符串[]参数)
{
XElement root=XElement.Parse(“”);
ReportMatchingNodeCount(root,“/a”);//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b”);//我希望有3个匹配项
ReportMatchingNodeCount(root,“/a/b/c”);//我希望有两个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='1']”;//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='2']”;//我希望有1个匹配项
Console.ReadLine();
}
私有静态void ReportMatchingNodeCount(XElement根,字符串xpath)
{
int matches=root.XPathSelectElements(xpath.Count();
控制台写入线(匹配);
}
}

我会从每个查询中删除前导正斜杠(/)。您可能还需要在XML的开头添加一个XML声明。见下文:

<?xml version="1.0" encoding="UTF-8"?>

我会从每个查询中删除前导正斜杠(/)。您可能还需要在XML的开头添加一个XML声明。见下文:

<?xml version="1.0" encoding="UTF-8"?>

我会从每个查询中删除前导正斜杠(/)。您可能还需要在XML的开头添加一个XML声明。见下文:

<?xml version="1.0" encoding="UTF-8"?>

我会从每个查询中删除前导正斜杠(/)。您可能还需要在XML的开头添加一个XML声明。见下文:

<?xml version="1.0" encoding="UTF-8"?>

之所以发生这种情况,是因为
XElement.Parse
元素作为当前节点返回。改用
XDocument.Parse

使用此代码,它将提供预期的结果

static void Main(string[] args)
{
    var root = XDocument.Parse("<a><b /><b /><b><c id='1'/><c id='2' /></b></a>");
    ReportMatchingNodeCount(root, "/a"); // I would expect 1 match
    ReportMatchingNodeCount(root, "/a/b"); // I would expect 3 matches
    ReportMatchingNodeCount(root, "/a/b/c"); // I would expect 2 matches
    ReportMatchingNodeCount(root, "/a/b/c[@id='1']"); // I would expect 1 match
    ReportMatchingNodeCount(root, "/a/b/c[@id='2']"); // I would expect 1 match
    Console.ReadLine();
}

private static void ReportMatchingNodeCount(XNode root, string xpath)
{
    int matches = root.XPathSelectElements(xpath).Count();
    Console.WriteLine(matches);
}
static void Main(字符串[]args)
{
var root=XDocument.Parse(“”);
ReportMatchingNodeCount(root,“/a”);//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b”);//我希望有3个匹配项
ReportMatchingNodeCount(root,“/a/b/c”);//我希望有两个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='1']”;//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='2']”;//我希望有1个匹配项
Console.ReadLine();
}
私有静态void ReportMatchingNodeCount(XNode根,字符串xpath)
{
int matches=root.XPathSelectElements(xpath.Count();
控制台写入线(匹配);
}

之所以发生这种情况,是因为
XElement.Parse
元素作为当前节点返回。改用
XDocument.Parse

使用此代码,它将提供预期的结果

static void Main(string[] args)
{
    var root = XDocument.Parse("<a><b /><b /><b><c id='1'/><c id='2' /></b></a>");
    ReportMatchingNodeCount(root, "/a"); // I would expect 1 match
    ReportMatchingNodeCount(root, "/a/b"); // I would expect 3 matches
    ReportMatchingNodeCount(root, "/a/b/c"); // I would expect 2 matches
    ReportMatchingNodeCount(root, "/a/b/c[@id='1']"); // I would expect 1 match
    ReportMatchingNodeCount(root, "/a/b/c[@id='2']"); // I would expect 1 match
    Console.ReadLine();
}

private static void ReportMatchingNodeCount(XNode root, string xpath)
{
    int matches = root.XPathSelectElements(xpath).Count();
    Console.WriteLine(matches);
}
static void Main(字符串[]args)
{
var root=XDocument.Parse(“”);
ReportMatchingNodeCount(root,“/a”);//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b”);//我希望有3个匹配项
ReportMatchingNodeCount(root,“/a/b/c”);//我希望有两个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='1']”;//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='2']”;//我希望有1个匹配项
Console.ReadLine();
}
私有静态void ReportMatchingNodeCount(XNode根,字符串xpath)
{
int matches=root.XPathSelectElements(xpath.Count();
控制台写入线(匹配);
}

之所以发生这种情况,是因为
XElement.Parse
元素作为当前节点返回。改用
XDocument.Parse

使用此代码,它将提供预期的结果

static void Main(string[] args)
{
    var root = XDocument.Parse("<a><b /><b /><b><c id='1'/><c id='2' /></b></a>");
    ReportMatchingNodeCount(root, "/a"); // I would expect 1 match
    ReportMatchingNodeCount(root, "/a/b"); // I would expect 3 matches
    ReportMatchingNodeCount(root, "/a/b/c"); // I would expect 2 matches
    ReportMatchingNodeCount(root, "/a/b/c[@id='1']"); // I would expect 1 match
    ReportMatchingNodeCount(root, "/a/b/c[@id='2']"); // I would expect 1 match
    Console.ReadLine();
}

private static void ReportMatchingNodeCount(XNode root, string xpath)
{
    int matches = root.XPathSelectElements(xpath).Count();
    Console.WriteLine(matches);
}
static void Main(字符串[]args)
{
var root=XDocument.Parse(“”);
ReportMatchingNodeCount(root,“/a”);//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b”);//我希望有3个匹配项
ReportMatchingNodeCount(root,“/a/b/c”);//我希望有两个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='1']”;//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='2']”;//我希望有1个匹配项
Console.ReadLine();
}
私有静态void ReportMatchingNodeCount(XNode根,字符串xpath)
{
int matches=root.XPathSelectElements(xpath.Count();
控制台写入线(匹配);
}

之所以发生这种情况,是因为
XElement.Parse
元素作为当前节点返回。改用
XDocument.Parse

使用此代码,它将提供预期的结果

static void Main(string[] args)
{
    var root = XDocument.Parse("<a><b /><b /><b><c id='1'/><c id='2' /></b></a>");
    ReportMatchingNodeCount(root, "/a"); // I would expect 1 match
    ReportMatchingNodeCount(root, "/a/b"); // I would expect 3 matches
    ReportMatchingNodeCount(root, "/a/b/c"); // I would expect 2 matches
    ReportMatchingNodeCount(root, "/a/b/c[@id='1']"); // I would expect 1 match
    ReportMatchingNodeCount(root, "/a/b/c[@id='2']"); // I would expect 1 match
    Console.ReadLine();
}

private static void ReportMatchingNodeCount(XNode root, string xpath)
{
    int matches = root.XPathSelectElements(xpath).Count();
    Console.WriteLine(matches);
}
static void Main(字符串[]args)
{
var root=XDocument.Parse(“”);
ReportMatchingNodeCount(root,“/a”);//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b”);//我希望有3个匹配项
ReportMatchingNodeCount(root,“/a/b/c”);//我希望有两个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='1']”;//我希望有1个匹配项
ReportMatchingNodeCount(root,“/a/b/c[@id='2']”;//我希望有1个匹配项
Console.ReadLine();
}
私有静态void ReportMatchingNodeCount(XNode根,字符串xpath)
{
int matches=root.XPathSelectElements(xpath.Count();
控制台写入线(匹配);
}

a
是根元素,因此您的XPath正在查找
/a/b

尝试:

报税表:3 2. 1.
1

a
是根元素,因此XPath正在查找
/a/b

尝试:

报税表:3 2. 1.
1

a
是根元素,因此XPath正在查找
/a/b

尝试:

报税表:3 2. 1.
1

a
是根元素,因此XPath正在查找
/a/b

尝试:

报税表:3 2. 1. 一,