C#使用指令解析xml失败

本文关键字:xml 失败 指令 | 更新日期: 2023-09-27 18:26:36

有人能告诉我为什么用指令导航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);

我在医生那里得到了一个例外。SelectSingleNode语句。

C#使用指令解析xml失败

在带有说明的版本中,您使用的是自定义命名空间。每个节点都将继承它,并且在请求节点数据时必须将其考虑在内。一种方法是使用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);
    }
}

还有其他选择。查看此博客文章了解更多详细信息。

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

        }
    }
}
​