0

我正在尝试创建一个列表,其中列表中的每个项目都包含包的名称和保存位置。我创建了一个具有两个变量名称和位置的构造函数。然后我创建了一个包含该构造函数的列表。我能够抓住这个名字,但这个位置给我带来了一些麻烦。此外,如果节点没有位置,那么我想要列表中该项目的空字符串。[请参阅我想要澄清的结果列表]。

我的 XML:

<project containsDynamicContent="true" xmlns="http://www.developer.cognos.com/schemas/bmt/60/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.developer.cognos.com/schemas/bmt/60/1 BMTModelSpecification.xsd">
    <packages>
        <package>
            <name>name1</name>
            <lastPublishedCMPath>Location_name1</lastPublishedCMPath>
        </package>
    </packages>
    <packages>
        <package>
            <name>name2</name>
        </package>
    </packages>
    <packages>
        <package>
            <name>name3<name>
            <lastPublishedCMPath>Location_name3</lastPublishedCMPath>
        </package>
    </packages>
</project>

我的 C# 代码:构造函数:

public class PackageNameAndLocation
    {
        public string PackageName { get; set; }
        public string LastLocation { get; set; }
    }

其余代码:

    private List<string> m_publishedCMPathList = new List<string>();
    private List<PackageNameAndLocation> m_pnalList = new List<PackageNameAndLocation>();

    Configuration.Instance.NSManager = new XmlNamespaceManager(xmlDoc.NameTable);
    Configuration.Instance.NSManager.AddNamespace("cg", mlDoc.DocumentElement.NamespaceURI);

    XmlNodeList m_packageName = xmlDoc.DocumentElement.SelectNodes("//cg:project/cg:packages/cg:package/cg:name", Configuration.Instance.NSManager);
    string m_lastLocation = string.Empty;

foreach (XmlNode name in m_packageName)
            {
                PackageNameAndLocation m_pnalClass = new PackageNameAndLocation();
                m_pnalClass.PackageName  = name.InnerText;
                XmlNode m_lastPublishedCMPath = name.SelectSingleNode("//cg:lastPublishedCMPath", Configuration.Instance.NSManager);
                if(m_lastPublishedCMPath != null)
                {
                    m_lastLocation = m_lastPublishedCMPath.InnerText;
                }
                else
                {
                    m_lastLocation = "";

                }
                m_pnalClass.LastLocation = m_lastLocation;
                m_pnalList.Add(m_pnalClass);
            }

My Result List:
[0] name1
    location_name1
[1] name2
    location_name1
[2] name3
    location_name1

Result List I want:
[0] name1
    location_name1
[1] name2
    "" [Empty String]
[2] name3
    location_name3

有人可以帮我吗?是 xpath 还是代码本身的问题?非常感谢!

4

1 回答 1

1

使用linq2xml ..它使用简单..

XElement doc=XElement.Load("yourXml.xml");
XNamespace ns="http://www.developer.cognos.com/schemas/bmt/60/1";

m_pnalList=doc.Descendants(ns+"package")
              .Select(d=>
                  new PackageNameAndLocation
                  {
                       PackageName=(string)d.Element(ns+"name"),
                       LastLocation=(string)d.Element(ns+"lastPublishedCMPath")
                  }
                  )
               .ToList<PackageNameAndLocation>();

如果你想坚持使用 xmldocument!

1>您不需要PackageNameAndLocation中的方法..只需保留属性

2>这应该做

XmlNodeList m_package = xmlDoc.DocumentElement.SelectNodes("//cg:package", Configuration.Instance.NSManager);
string m_lastLocation = string.Empty;

foreach (XmlNode package in m_package)
            {
                PackageNameAndLocation m_pnalClass = new PackageNameAndLocation();
                m_pnalClass.PackageName  = package.SelectSingleNode("//cg:name").InnerText;
                XmlNode m_lastPublishedCMPath =  name.SelectSingleNode("//cg:lastPublishedCMPath", Configuration.Instance.NSManager);
.....
于 2013-07-16T17:50:56.477 回答