文档根节点的名称

文档根节点的名称

本文介绍了获取 XML 文档根节点的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要加载的 XML 文件.我想在我的代码中放置一个条件来检查并确保根节点是TimeLog".如果不是,则告诉用户他们选择了错误的 XML 文件.

I have an XML file that I want to load. I want to put a condition in my code to check and make sure the root node is 'TimeLog'. If it isn't then tell the user that they picked the wrong XML file.

在下面的代码中,r.Name 只返回一个空字符串.我希望它返回TimeLog",但它没有.

In the code below, r.Name only returns an empty string. I'd expect it to return "TimeLog" but it does not.

    private void loadFromFile(object sender, EventArgs e)
    {
        if(File.Exists("TimeLog.xml"))
        {
            XmlReader r = XmlReader.Create("TimeLog.xml");
            r.MoveToContent();
            r.ReadStartElement();
            if (r.Name == "TimeLog") //The current value or r.name is "".
            {
                while (r.Read())
                {
                    if (r.NodeType == XmlNodeType.Element)
                    {
                        r.ReadToDescendant("EntryName");
                        String name = r.ReadInnerXml();
                        r.ReadToFollowing("StartDateTime");
                        String start = r.ReadInnerXml();
                        r.ReadToFollowing("EndDateTime");
                        String end = r.ReadInnerXml();
                        r.ReadToFollowing("Duration");
                        String dur = r.ReadInnerXml();

                        entries.Add(new Entry(name, start, end, dur));
                    }
                }
            }
            else
            {
                MessageBox.Show("This is not a TimeLog XML file", "This is not a TimeLog XML file", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            r.Close();
        }
    }

XML 文件是我的 saveToFile() 函数的输出,如下所示

The XML file is the output my saveToFile() function and looks like this

<?xml version="1.0" encoding="utf-8"?>
<TimeLog>
    <Entry>
        <EntryName>Entry 01</EntryName>
        <StartDateTime>8/21/2015 8:50:40 PM</StartDateTime>
        <EndDateTime>1/1/0001 12:00:00 AM</EndDateTime>
        <Duration>00:00:00</Duration>
    </Entry>
    <Entry>
        <EntryName>Entry 02</EntryName>
        <StartDateTime>8/21/2015 8:50:40 PM</StartDateTime>
        <EndDateTime>1/1/0001 12:00:00 AM</EndDateTime>
        <Duration>00:00:00</Duration>
    </Entry>
</TimeLog>

如何在不调用另一个 .Read() 函数并为 while 语句中的代码抛出顺序的情况下检查以确保根节点的名称是TimeLog"?

How can I check to make sure the name of the Root Node is "TimeLog" without calling another .Read() function and throwing the order off for the code in the while statement?

推荐答案

ReadStartElement 方法说明:

检查当前节点是否是一个元素并将阅读器推进到下一个节点.

下一个节点是 XmlNodeType.Whitespace.

考虑以下 XML:

<TimeLog>
    <Entry>

也可以写成:

<TimeLog>    <Entry>

两个XmlNodeType.Element之间有XmlNodeType.Whitespace.

MoveToContent 将 reader 设置为根节点 - TimeLog.然后 ReadStartElement 读取 TimeLog 并移动到空白处.

MoveToContent sets reader to the root node - TimeLog. Then ReadStartElement reads TimeLog and moves to the whitespace.

以下视图中没有空格:

<TimeLog><Entry>

顺便说一句,您可以使用 ReadToFollowing 方法:

using (var reader = XmlReader.Create("file.xml"))
{
    reader.ReadToFollowing("TimeLog");
    // rest of code
}

这篇关于获取 XML 文档根节点的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:09