Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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#:获取属性';xml元素的xpath_C#_Xml_Xpath_Xml Parsing - Fatal编程技术网

c#:获取属性';xml元素的xpath

c#:获取属性';xml元素的xpath,c#,xml,xpath,xml-parsing,C#,Xml,Xpath,Xml Parsing,给定以下XML: <enrollment> <school> <students> <studentA fname="John" lname="Doe" age="23" /> <studentB fname="Mary" lname="Johnson" age="22" /> </students> </school

给定以下XML:

<enrollment>
    <school>
        <students>
            <studentA fname="John" lname="Doe" age="23" />
            <studentB fname="Mary" lname="Johnson" age="22" /> 
        </students>  
    </school>  
</enrollment>
Where node=“studentA”,如何获取每个属性的XPath

编辑:
基本上,我在这里试图实现的是比较两个节点是否相同。因此,我必须检查它们是否具有相同的名称、属性和属性值。因此,给定一个节点,我需要一个与所述条件相匹配的xpath表达式。

您可以直接将其与所有studentA节点一样放置-

Xpath-
“//studentA”

或在特定节点上迭代- Xpath-
“注册/学校/学生/studentA”

如果要查找属性fname

Xpath-
“注册/学校/学生/学生塔[@fname]”

假设myXml是您的XML文档 您可以迭代特定的节点属性,如-

        System.Xml.XmlNode xn =  myXml.SelectSingleNode("enrollment/school/students/studentA");
        foreach (System.Xml.XmlAttribute attrib in xn.Attributes)
        {
            // find attribute name using attrib.Name
            string sAttribName = attrib.Name;
            if (sAttribName == "fname")
            {
                //Check your codes here
            }               
        }

@属性用于属性选择。

检查它们是否具有“相同的名称、属性和属性值”的哪一部分需要知道属性的XPath?@JLRishi我忘了提到要比较的两个节点来自不同的XML文件。给定xml1上的一个节点,我将获取其xpath并使用它来查找xml2上是否存在类似的节点。但是,谢谢你把事情说清楚,我意识到我真正需要的是节点的xpath,属性是xpath表达式的条件。Rohit的答案是正确的。我的错了。它应该是registration/school/studentA[@fname]+1。为此,我现在可以获取具有fname属性的元素,但是如果元素具有多个属性,您还可以向我显示一个匹配的xpath表达式。e、 g fname,lname,age@wintersolstice,您使用的是XMLDocument还是Linq to XML?我使用的是XMLDocument,我发现xpath也有逻辑运算符。e、 “和”和“或”,我想我可以用它
        System.Xml.XmlNode xn =  myXml.SelectSingleNode("enrollment/school/students/studentA");
        foreach (System.Xml.XmlAttribute attrib in xn.Attributes)
        {
            // find attribute name using attrib.Name
            string sAttribName = attrib.Name;
            if (sAttribName == "fname")
            {
                //Check your codes here
            }               
        }
You can use enrollment/school/students/studentA/@fname