C#使用指令解析xml失败

时间:2015-12-12 19:47:45

标签: c# xml xpath

有人能告诉我为什么用指令导航xml会失败:

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

这很有效,但带有指令的XML相同失败:

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

我在doc.SelectSingleNode语句中遇到异常。

2 个答案:

答案 0 :(得分:1)

在包含说明的版本中,您使用的是自定义命名空间。每个节点都将继承该节点,并且在请求节点数据时必须将其考虑在内。一种方法是使用XmlNamespaceManager。下面是应用管理器的代码版本:

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

还有其他选择。有关详细信息,请查看此blog post

答案 1 :(得分:0)

以下是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();


        }
    }
}
​