Chewdoggie
Chewdoggie

Reputation: 449

Can't read XML "value"

I have some XML that looks like this:

<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>

I'm trying to capture the Value of the second "Name" node ("ProcessAgility").

But (XmlReader) reader.Value returns an empty string when I arrive at this node. How do I capture the TEXT that falls betweeen <nodeName>TEXT</nodeName> ?

Here's my code so far:

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;
    }
}

Upvotes: 0

Views: 84

Answers (2)

Zack
Zack

Reputation: 2859

A pretty simple (and very specific) System.Xml.Linq.XElement solution would be:

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

Or if you include a using System.Xml.XPath directive, you could use XPath natigation with the XPathSelectElement extension method:

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

Upvotes: 0

montelof
montelof

Reputation: 501

Try using XmlDocument then you can use XPath navigation like this: /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);

This will give you the first node, if you want the list then iterate it you can use:

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

Upvotes: 2

Related Questions