获取XML文档根节点的名称

问题描述 投票:0回答:4

我有一个要加载的XML文件。我想在我的代码中添加一个条件以检查并确保根节点为“ TimeLog”。如果不是,则告诉用户他们选择了错误的XML文件。

在下面的代码中,r.Name仅返回一个空字符串。我希望它返回“ TimeLog”,但不会。

    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()函数的输出,看起来像这样

<?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()函数的情况下确保根节点的名称为“ TimeLog”,并在while语句中放弃代码的顺序?

c# xml xmlreader
4个回答
0
投票

检查当前节点是否为元素,并使阅读器前进至下一个节点。

下一个节点是XmlNodeType.Whitespace

考虑以下XML:


<TimeLog> <Entry>

也可以写成:

<TimeLog>    <Entry>

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

MoveToContent将阅读器设置为根节点-TimeLog。然后ReadStartElement读取TimeLog并移至空白。

以下视图中没有空格:


<TimeLog><Entry>

顺便说一句,您可以使用ReadToFollowing方法:
using (var reader = XmlReader.Create("file.xml")) { reader.ReadToFollowing("TimeLog"); // rest of code }

0
投票
if(File.Exists("TimeLog.xml")) { XmlReader r = XmlReader.Create("TimeLog.xml"); r.MoveToContent(); //Here check whether there exist a root element and then also check itsname. //If this doesn't work out then put the r.Name=="TimeLog" inside While loop and then see. if (r.IsStartElement() && string.Equals(r.Name, "TimeLog")) { 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)); }//End of inner If }//End of While }//End of Outer second IF else { MessageBox.Show("This is not a TimeLog XML file", "This is not a TimeLog XML file", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); }//End of Else r.Close();

0
投票
XDocument doc = XDocument.Load("TimeLog.xml"); if (doc.Root.Name == "TimeLog") { // ... } else { MessageBox.Show("This is not a TimeLog XML file", "This is not a TimeLog XML file", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); }

0
投票
element。通过使用XmlReader而不是XmlDocument,不必将整个文档加载到内存中。

if (File.Exists("TimeLog.xml")) { XmlReader r = XmlReader.Create("TimeLog.xml"); while (reader.Read() && reader.NodeType != XmlNodeType.Element) ; // r is now referencing the node for the root element. } 有效XML文档中的根

node
是XML声明,可能不是您想要的。

© www.soinside.com 2019 - 2024. All rights reserved.