【问题标题】:How would I retrieve Query Parameter and name Values From XML String如何从 XML 字符串中检索查询参数和名称值
【发布时间】:2014-08-11 03:33:43
【问题描述】:

我将如何解析以下内容,它至少不会改变行的布局。所以我需要能够获取查询参数值,即 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> 

【问题讨论】:

  • 问题中的标点符号会有所帮助
  • 一个像样的问题标题也会有所帮助
  • 那么你想获取一个节点的属性值吗?尝试这个! stackoverflow.com/questions/933687/…
  • 是的,这是正确的,@DavidS。对不起,我有拼写障碍

标签: c# xml regex parsing


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,但无法实现它是他们无法将其解析出来的,因为我将所有这些都作为一个字符串,但它们可能是多个参数
  • 更新了代码以适应有多个&lt;QueryParameter&gt;s的情况
  • 我认为这太复杂了我已经在强类型类“" 我需要做的就是提取 typeprotectionid 和 name 的值: 我不能只解析这一行吗?
猜你喜欢
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
相关资源
最近更新 更多