foreach xmlnode chooseinglenode在c#中无法正常工作

时间:2013-07-16 17:36:53

标签: c# xml list foreach selectsinglenode

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

我的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还是代码本身?非常感谢你!

1 个答案:

答案 0 :(得分: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&gt;您不需要PackageNameAndLocation中的方法。只需保留属性

2&gt;这应该

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