【问题标题】:C# - Read attributes from XML with sub XmlnsC# - 使用子 Xmlns 从 XML 读取属性
【发布时间】:2017-08-19 07:26:00
【问题描述】:

关于 XML 的 Newbi。 试图从所有 DisplayName:s 中读取 Text 属性。认为我的问题在 Rule 元素附近。有没有办法解决我的问题?提前致谢! //马格纳斯

<?xml version="1.0" encoding="utf-16"?>
<AppMgmtDigest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/SystemCenterConfigurationManager/2009/AppMgmtDigest">
  <DeploymentType >
    <Requirements>
      <Rule xmlns="http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/14/Rules">
        <Annotation>
          <DisplayName Text="Primary device Equals True"/>
        </Annotation>
      </Rule>
      <Rule xmlns="http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/14/Rules">
        <Annotation>
          <DisplayName Text="Operating system One of {All Windows 10 (64-bit)}"/>
        </Annotation>
      </Rule>
    </Requirements>
  </DeploymentType>
</AppMgmtDigest>





XmlDocument xml = new XmlDocument();
xml.LoadXml(SDMPackageXML);

XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("msbld","http://schemas.microsoft.com/SystemCenterConfigurationManager/2009/AppMgmtDigest");
ns.AddNamespace("msbldr","http://schemas.microsoft.com/SystemCenterConfigurationManager/2009/06/14/Rules");

XmlNodeList nodlist = xml.SelectNode("/msbld:AppMgmtDigest/msbld:DeploymentType/msbld:Requirements/msbldr:Rule", ns/@Text);

【问题讨论】:

  • 看起来是合理的起点。澄清您在使用此代码时遇到的确切问题会有所帮助。
  • 嗨,阿列克谢!问题是 nodlist Count 为 0。我期望 Count 为 2。 schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/…"> schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/…">

标签: c# xml xpath


【解决方案1】:

试试 xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            reader.ReadLine(); //skip the identification line with utf-16
            XElement doc = XElement.Load(reader);

            List<string> text = doc.Descendants().Where(x => x.Name.LocalName == "DisplayName").Select(x => (string)x.Attribute("Text")).ToList();

        }

    }
}

【讨论】:

    【解决方案2】:

    首先,您在命名空间中有一个错字:Systems 在 xml 中,System 在 C# 中。

    其次,您的代码无法编译。在发布之前,请更正明显的错误。

    请试试这个:

    // Correct typo
    ns.AddNamespace("msbldr",
        "http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/14/Rules");
    
    XmlNodeList nodelst = xml.SelectNodes(
        "/msbld:AppMgmtDigest/msbld:DeploymentType/msbld:Requirements/msbldr:Rule//@Text", ns);
    

    此外,您的 xpath 可能会指定所需属性的确切路径:

    "/msbld:AppMgmtDigest/msbld:DeploymentType/msbld:Requirements/msbldr:Rule/msbldr:Annotation/msbldr:DisplayName/@Text"
    

    【讨论】:

      猜你喜欢
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 2022-11-12
      • 2018-05-21
      • 2012-07-29
      相关资源
      最近更新 更多