XMLTextReader 不读取元素内容

发布于 2024-09-27 16:04:18 字数 1183 浏览 4 评论 0原文

static void ReadXml()
{
    string a= null;
    double b= 0;
    double c= 0;
    XmlReader xmlReader = new XmlReader("Testxml.xml");
    xmlReader.
    using (xmlReader)
    {
        if (xmlReader != null)
        { 
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    switch (xmlReader.Name)
                    {
                        case "a":
                            a = xmlReader.ReadElementContentAsString();

                            break;
                        case "b":
                            b = double.Parse(xmlReader.ReadElementContentAsString());

                            break;
                        case "c":
                            c = double.Parse(xmlReader.ReadElementContentAsString());

                            break;
                    }
                }
            }
        }
    }
}

TestXML 内容:

<a><b>26a83f12c782</b><c>128</c><d>12</d></a>

情况 b 从未被命中。但是如果我在 b 的结束元素后面添加一个空格,则会遇到情况 b。现在如何让它在不更改 xml 文件的情况下工作?

static void ReadXml()
{
    string a= null;
    double b= 0;
    double c= 0;
    XmlReader xmlReader = new XmlReader("Testxml.xml");
    xmlReader.
    using (xmlReader)
    {
        if (xmlReader != null)
        { 
            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Element)
                {
                    switch (xmlReader.Name)
                    {
                        case "a":
                            a = xmlReader.ReadElementContentAsString();

                            break;
                        case "b":
                            b = double.Parse(xmlReader.ReadElementContentAsString());

                            break;
                        case "c":
                            c = double.Parse(xmlReader.ReadElementContentAsString());

                            break;
                    }
                }
            }
        }
    }
}

TestXML content:

<a><b>26a83f12c782</b><c>128</c><d>12</d></a>

Case b is never hit. But If I add a space after end element of b, case b is hit. Now how to make it work without changing the xml file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名的好友 2024-10-04 16:04:19

这是您的代码的工作版本。已更正的具体问题包括:

  1. 新 XmlReader 无法编译。这是一个抽象类。您需要使用 XmlTextReader 或另一个 XmlReader 派生类。

  2. b 不是有效的双精度值。您试图将一个大的十六进制数直接转换为双精度数,这是不可能的。您可以在 Parse 调用中使用 NumberStyles.HexNumber,但不能使用 double,它必须是 longint

  3. 双重阅读。您在循环内调用了 Read(),但随后也使用了 XmlReader.ReadXxx() 方法。这就是调用额外的读取时间并跳过节点。这确实是您问的主要问题。以下代码跟踪找到的最后一个元素,然后等待它到达 Text 节点进行处理。这对于简单/平面文档来说很好,但对于更复杂的文档,您需要更好的方法来跟踪状态,例如有限状态机。或者使用 DOM。或者 LINQ。

    static void ReadXml()
    {
        字符串 a = null;
        长b=0;
        双c = 0;
        字符串文本=“26a83f12c78212812”;
        字符串元素=“”;
    
    
    
    using (XmlReader xmlReader = new XmlTextReader(new StringReader(text)))
    {
        while (xmlReader.Read())
        {
            if (xmlReader.NodeType == XmlNodeType.Element)
            {
                元素 = xmlReader.Name;
            }
            else if (xmlReader.NodeType == XmlNodeType.Text)
            {
                开关(元件)
                {
                    情况“a”:
                        a = xmlReader.Value;
                        Console.WriteLine("a: " + a);
                        休息;
                    情况“b”:
                        b = long.Parse(xmlReader.Value, NumberStyles.HexNumber);
                        Console.WriteLine("b: " + b);
                        休息;
                    情况“c”:
                        c = double.Parse(xmlReader.Value);
                        Console.WriteLine("c: " + c);
                        休息;
                }
            }
        }
    }
    

    }

Here's a working version of your code. The specific problems that are corrected include:

  1. new XmlReader doesn't compile. It's an abstract class. You need to use XmlTextReader or another XmlReader derived class.

  2. b is not a valid double. You were trying to convert a large hex number to a double directly which isn't possible. You can use NumberStyles.HexNumber in the Parse call, but not with double, it has to be long or int.

  3. Double read. You were calling Read() inside a loop but then using the XmlReader.ReadXxx() methods as well. This was calling read extra times and skipping nodes. This is really the main problem you were asking about. The following code keeps track of the last element found and then waits till it hits the Text node for processing. This is fine for simple/flat documents, but for more complex ones you need a better way of keeping track of state, like a finite state machine. Or use DOM. Or LINQ.

    static void ReadXml()
    {
        string a = null;
        long b = 0;
        double c = 0;
        string text = "<a><b>26a83f12c782</b><c>128</c><d>12</d></a>";
        string element = "";
    
    
    
    using (XmlReader xmlReader = new XmlTextReader(new StringReader(text)))
    {
        while (xmlReader.Read())
        {
            if (xmlReader.NodeType == XmlNodeType.Element)
            {
                element = xmlReader.Name;
            }
            else if (xmlReader.NodeType == XmlNodeType.Text)
            {
                switch (element)
                {
                    case "a":
                        a = xmlReader.Value;
                        Console.WriteLine("a: " + a);
                        break;
                    case "b":
                        b = long.Parse(xmlReader.Value, NumberStyles.HexNumber);
                        Console.WriteLine("b: " + b);
                        break;
                    case "c":
                        c = double.Parse(xmlReader.Value);
                        Console.WriteLine("c: " + c);
                        break;
                }
            }
        }
    }
    

    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文