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

本文关键字:参数 查询 检索 XML 字符串 | 更新日期: 2023-09-27 18:09:35

我将如何解析以下内容,它至少不会改变该行的布局。所以我需要能够得到查询参数值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字符串中检索查询参数和名称值

您的XML具有默认名称空间,因此您需要使用XmlNamespaceManager才能使用XPath查询XML。例如,要获取Parameter属性的值,您可以这样做:

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);
}