无法读取XML“价值”

时间:2016-05-18 14:56:41

标签: c# xml

我有一些看起来像这样的XML:

<PackageConstruct900 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ID>{5209724e-1c5a-4d84-962e-371271c3836c}</ID>
  <ParentID />
  <Name />
  <Type>Package</Type>
  <Tasks>
    <anyType xsi:type="Task">
      <ID>{4c97132c-ba7d-4fba-9b01-333976e9ad22}</ID>
      <ParentID>{E893A7FD-2758-4315-9181-93F8728332E5}</ParentID>
      <Name>ProcessAgility</Name>
      <Type>Task</Type>
      <StartedOn>1900-01-01T00:00:00</StartedOn>
      <EndedOn>1900-01-01T00:00:00</EndedOn>
    </anyType>
  </Tasks>
</PackageConstruct900>

我正在尝试捕获第二个“名称”节点的值(“ProcessAgility”)。

但是(XmlReader)reader.Value在到达此节点时返回一个空字符串。如何捕获<nodeName>TEXT</nodeName>之间的文本?

到目前为止,这是我的代码:

XmlReader reader = XmlReader.Create(pathToFile, settings);

reader.MoveToContent();
while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Text:
            break;
        case XmlNodeType.Element:                        
            switch (reader.Name)
            {
                case "anyType":
                    newJob = true;
                    break;
                case "AML":
                    string ss = string.Empty;
                    ss = reader.ReadInnerXml();
                    ss = System.Net.WebUtility.HtmlDecode(ss);
                    rs = XmlReader.Create(ss, settings);
                    break;
                case "Name":
                    if (newJob && reader.HasValue)
                    {
                        jobName = reader.Value;
                    }
                    if (!string.IsNullOrWhiteSpace(jobName))
                    {
                        if (!jobsAdded.Contains(jobName))
                        {
                            jobsAdded.Add(jobName);
                        }
                    }
                    break;
                case "Tasks":
                    m_ConvertingTask = true;
                    break;
                case "TRIGGERS":
                    break;
            }
            break;
    }
}

2 个答案:

答案 0 :(得分:2)

尝试使用XmlDocument然后你可以像这样使用XPath导航:/ PackageConstruct900 / Tasks / anyType / Name

        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<PackageConstruct900 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n  <ID>{5209724e-1c5a-4d84-962e-371271c3836c}</ID>\r\n  <ParentID />\r\n  <Name />\r\n  <Type>Package</Type>\r\n  <Tasks>\r\n    <anyType xsi:type=\"Task\">\r\n      <ID>{4c97132c-ba7d-4fba-9b01-333976e9ad22}</ID>\r\n      <ParentID>{E893A7FD-2758-4315-9181-93F8728332E5}</ParentID>\r\n      <Name>ProcessAgility</Name>\r\n      <Type>Task</Type>\r\n      <StartedOn>1900-01-01T00:00:00</StartedOn>\r\n      <EndedOn>1900-01-01T00:00:00</EndedOn>\r\n    </anyType>\r\n  </Tasks>\r\n</PackageConstruct900>");
        XmlNode root = doc.DocumentElement;
        XmlNode node = root.SelectSingleNode(
            "/PackageConstruct900/Tasks/anyType/Name");
        Console.WriteLine(node.InnerXml);

这将为您提供第一个节点,如果您想要列表然后迭代它,您可以使用:

        XmlNodeList nodes = root.SelectNodes("/PackageConstruct900/Tasks");
        foreach (XmlNode node in nodes)
        {
            var typename = node.SelectSingleNode("anyType/Name");
            Console.WriteLine(typename.InnerXml);    
        }            

答案 1 :(得分:0)

一个非常简单(非常具体)的System.Xml.Linq.XElement解决方案是:

string processAgility = XElement.Parse(File.ReadAllText(pathToFile))
  .Element("Tasks")
  .Element("anyType")
  .Element("Name")
  .Value;

或者,如果您包含using System.Xml.XPath指令,则可以使用XPathSelectElement扩展方法使用XPath natigation:

string processAgility = XElement.Parse(File.ReadAllText(pathToFile))
  .XPathSelectElement("Tasks/anyType/Name")
  .Value;