繁体   English   中英

C#使用指令解析xml失败

[英]C# parsing xml with instruction fails

谁能告诉我为什么使用指令导航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语句中遇到异常。

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


        }
    }
}
​

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM