Steef
Steef

Reputation: 615

C# parsing xml with instruction fails

Can anybody tell me why navigating xml with an instruction fails :

StringBuilder   sb2 = new System.Text.StringBuilder();
XmlDocument     doc = new XmlDocument( );

// --- XML without instruction -> Parsing succeeds 
sb1.AppendLine( @"<MetalQuote>");
sb1.AppendLine( @"<Outcome>Success</Outcome>");
sb1.AppendLine( @"<Ask>1073.3</Ask>");
sb1.AppendLine( @"</MetalQuote>");

doc.LoadXml( sb1.ToString( ));
System.Diagnostics.Debug.WriteLine( doc.SelectSingleNode( "//MetalQuote/Outcome").InnerText);

This works well, but the same XML with an instruction fails :

// --- XML with instruction -> Parsing fails 
sb2.AppendLine( @"<MetalQuote xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.xignite.com/services"" >");
sb2.AppendLine( @"<Outcome>Success</Outcome>");
sb2.AppendLine( @"<Ask>1073.3</Ask>");
sb2.AppendLine( @"</MetalQuote>");

doc.LoadXml( sb2.ToString( ));
System.Diagnostics.Debug.WriteLine( doc.SelectSingleNode( "//MetalQuote/Outcome").InnerText);

I get an exception at the doc.SelectSingleNode statement.

Upvotes: 0

Views: 67

Answers (2)

PiotrWolkowski
PiotrWolkowski

Reputation: 8792

In your version with instructions you are using a custom namespace. Each node will inherit that and you have to take it into account when requesting the node data. One way to do it is to use XmlNamespaceManager. Below a version of your code that applies the manager:

class Program
{
    static void Main(string[] args)
    {
        StringBuilder sb2 = new System.Text.StringBuilder();
        XmlDocument doc = new XmlDocument();

        // --- XML with instruction -> Parsing fails 
        sb2.AppendLine(@"<MetalQuote xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.xignite.com/services"" >");
        sb2.AppendLine(@"<Outcome>Success</Outcome>");
        sb2.AppendLine(@"<Ask>1073.3</Ask>");
        sb2.AppendLine(@"</MetalQuote>");
        doc.LoadXml(sb2.ToString());

        // Create a manager
        XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
        xnm.AddNamespace("abc", @"http://www.xignite.com/services");

        // Use the namespace for each node
        System.Diagnostics.Debug
            .WriteLine(doc.SelectSingleNode(@"//abc:MetalQuote/abc:Outcome", xnm).InnerText);
    }
}

There other options available as well. Check this blog post for more details.

Upvotes: 1

jdweng
jdweng

Reputation: 34431

Here are two ways with XML Linq

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb2 = new System.Text.StringBuilder();

            sb2.AppendLine(@"<MetalQuote xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://www.xignite.com/services"" >");
            sb2.AppendLine(@"<Outcome>Success</Outcome>");
            sb2.AppendLine(@"<Ask>1073.3</Ask>");
            sb2.AppendLine(@"</MetalQuote>");

            XDocument doc = XDocument.Parse(sb2.ToString());
            XElement outCome = doc.Descendants().Where(x => x.Name.LocalName == "Outcome").FirstOrDefault();

            XElement metalQuote = (XElement)doc.FirstNode;
            XNamespace ns = metalQuote.Name.Namespace;
            XElement outCome2 = doc.Descendants(ns + "Outcome").FirstOrDefault();


        }
    }
}
​

Upvotes: 0

Related Questions