Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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字符串中检索查询参数和名称值_C#_Xml_Regex_Parsing - Fatal编程技术网

C# 如何从XML字符串中检索查询参数和名称值

C# 如何从XML字符串中检索查询参数和名称值,c#,xml,regex,parsing,C#,Xml,Regex,Parsing,我将如何解析以下内容,它至少不会改变行的布局。所以我需要能够从ie TypeProjectionId中获取查询参数值,以及Name=?有人有什么想法吗 我使用它从xml文件中获取文本 string path = "/View/Data/ItemsSource"; XmlNodeList nodeList = currentDocument.SelectNodes(path); IDictionary&

我将如何解析以下内容,它至少不会改变行的布局。所以我需要能够从ie TypeProjectionId中获取查询参数值,以及Name=?有人有什么想法吗

我使用它从xml文件中获取文本

                string path = "/View/Data/ItemsSource";
                XmlNodeList nodeList = currentDocument.SelectNodes(path);
                IDictionary<string, string> keyValuePairList = new Dictionary<string, string>();
                string itemsource;
                itemsource = "";
                foreach (XmlNode node in nodeList)
                {

                    itemsource = node.InnerXml;
                     //   keyValuePairList.Add(new KeyValuePair<string, string>(node.Attributes[0].Value, node.Attributes[0].Value));

                }
itemsource的结果

    <AdvancedListSupportClass xmlns=\"clr-namespace:Microsoft.EnterpriseManagement.UI.ViewFramework;assembly=Microsoft.EnterpriseManagement.UI.ViewFramework\" xmlns:av=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:s=\"clr-namespace:System;assembly=mscorlib\" DataTypeName=\"\" AdapterName=\"viewframework://Adapters/AdvancedList\" FullUpdateAdapter=\"dataportal:EnterpriseManagementObjectProjectionAdapter\" DataSource=\"mom:ManagementGroup\" IsRecurring=\"True\" RecurrenceFrequency=\"{x:Static s:Int32.MaxValue}\" FullUpdateFrequency=\"1\" Streaming=\"true\">
      <AdvancedListSupportClass.Parameters>
        <QueryParameter Parameter=\"TypeProjectionId\" Value=\"$MPElement[Name='System.WorkItem.Incident.View.ProjectionType']$\" />
      </AdvancedListSupportClass.Parameters>
    </AdvancedListSupportClass> 

XML具有默认名称空间,因此需要使用XmlNamespaceManager才能使用XPath查询XML。例如,要获取参数属性的值,可以执行以下操作:

XmlNamespaceManager nsManager = new XmlNamespaceManager(new NameTable());

//register ns prefix to point to default namespace 
nsManager.AddNamespace("ns", node.FirstChild.GetNamespaceOfPrefix(""));

//use the namespace manager and registered prefix to get desired element
string xpath = "/ns:AdvancedListSupportClass/ns:AdvancedListSupportClass.Parameters/ns:QueryParameter";
var queryParameters = node.SelectNodes(xpath, nsManager);

foreach(XmlNode queryParameter in queryParameters)
{
    //get value of Parameter attribute of each <QueryParameter>
    Console.WriteLine(queryParameter.Attributes["Parameter"].Value);
}

问题中的标点符号会有帮助一个好的问题标题也会有帮助所以你想得到一个节点的AttributeValue?试试这个!是的,这是正确的,@DavidS。很抱歉我在拼写上有诵读困难谢谢你的答案但在表达上有困难他们没有办法我可以把它的值解析出来因为我得到的是一个字符串,他们可以是多个paremters虽然更新了代码以适应有多个的情况我认为这太复杂了我在强类型类中已经有了它,我需要做的就是提取typeprotectionid和name的值:我能不能不解析这一行?