Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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 - Fatal编程技术网

C#XML-XPath查询生成器-动态创建查询

C#XML-XPath查询生成器-动态创建查询,c#,xml,xpath,C#,Xml,Xpath,我正试图构建一个XPath查询生成器,以使通用代码尽可能可移植 到目前为止,我的想法是: private static string XpathQueryBuilder (string NodeName,string AttributeName = null, string AttributeValue = null) { string XpathAttr = ""; if (AttributeName != null) if

我正试图构建一个XPath查询生成器,以使通用代码尽可能可移植

到目前为止,我的想法是:

private static string XpathQueryBuilder (string NodeName,string AttributeName = null, string AttributeValue = null)
    {

        string XpathAttr = "";

        if (AttributeName != null)
            if (AttributeValue != null)
                XpathAttr = "[@" + AttributeName + "='" + AttributeValue + "']";
            else
                XpathAttr = "[@" + AttributeName + "='*']";
        return "//" + NodeName + XpathAttr;

    }    
但是,我看到这个方法的问题是,如果我想查找多个属性或节点,那么这个函数将无法工作。是否有一种动态创建XPath查询的方法,该查询在理论上可以接受任意数量的属性和/或节点

我的优先考虑是拥有一个接受多个属性和属性值的函数,因为这是比多个节点更可能的情况


谢谢你抽出时间

您是否尝试过使用LINQ转换XML

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

String GUID = "something";

XElement profilesXel = XElement.Load("your xml file path");
XElement currProfile = (from el in profilesXel
                        where (String)el.Element("GUID") == GUID
                        select el).First();

..

您是否尝试过使用LINQ转换XML

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

String GUID = "something";

XElement profilesXel = XElement.Load("your xml file path");
XElement currProfile = (from el in profilesXel
                        where (String)el.Element("GUID") == GUID
                        select el).First();
..

您可以使用

它将允许您选择所需的任何数据

此外,如果您需要更通用的解决方案,您可以尝试为此实现

第二种方法比第一种更复杂,但因此您将拥有更通用的解决方案,该解决方案将通过LINQ链和表达式(lambda等)提供对xml文件的访问

提供一些示例链接以获取帮助:

您可以使用

它将允许您选择所需的任何数据

此外,如果您需要更通用的解决方案,您可以尝试为此实现

第二种方法比第一种更复杂,但因此您将拥有更通用的解决方案,该解决方案将通过LINQ链和表达式(lambda等)提供对xml文件的访问

提供一些示例链接以获取帮助:


您可以使用
字典
使函数能够接收多个属性参数:

private static string XpathQueryBuilder(string nodeName, Dictionary<string,string> attributes = null)
{
    string xpathAttr = "";
    if (attributes != null)
    {
        xpathAttr = 
            "[" + 
            String.Join(" and ", 
                    attributes.Select(o => 
                    {
                        var attrVal = o.Value ?? "*";
                        return "@" + o.Key + "='" + attrVal + "'";
                    })
            ) + "]";
    }

    return "//" + nodeName + xpathAttr;
}   

您可以使用
字典
使函数能够接收多个属性参数:

private static string XpathQueryBuilder(string nodeName, Dictionary<string,string> attributes = null)
{
    string xpathAttr = "";
    if (attributes != null)
    {
        xpathAttr = 
            "[" + 
            String.Join(" and ", 
                    attributes.Select(o => 
                    {
                        var attrVal = o.Value ?? "*";
                        return "@" + o.Key + "='" + attrVal + "'";
                    })
            ) + "]";
    }

    return "//" + nodeName + xpathAttr;
}   

你能回答得更具体些吗?我很难弄明白这一点,我必须诚实地说,我不知道从Linq库开始。你能更具体地回答吗?我很难弄明白这一点,老实说,我不知道从Linq库的什么地方开始。为什么不传递一个KeyValuePair,传递一个列表,然后通过附加属性来循环它呢?这将是一个很好的选择,而不是创建自定义Linq提供程序。我不知道密钥对机制。谢谢。为什么不传递一个KeyValuePair,传递一个列表,然后通过附加属性来循环它呢?这将是一个很好的选择,而不是创建一个自定义Linq提供程序。我不知道密钥对机制。谢谢。谢谢你提供的链接,我肯定会研究LINQ到XML库,但现在,我觉得我能管理的有点过了,所以我将使用词汇法。当然,我现在不会强迫你加入。我很乐意帮助您将LINQ理解为XML。或者,如果您决定实现自己的提供商,请随时询问!谢谢你们提供的链接,我肯定会研究LINQ到XML库,但现在,我觉得它有点超出了我的能力范围,所以我将使用字典法。当然,我不会强迫你们现在就跳进去。我很乐意帮助您将LINQ理解为XML。或者,如果您决定实现自己的提供商,请随时询问!