获取 XML 文档根节点的名称
Getting the name of an XML document's root node
我有一个 XML 文件要加载。我想在我的代码中添加一个条件来检查并确保根节点是 'TimeLog'。如果不是,则告诉用户他们选择了错误的 XML 文件。
在下面的代码中,r.Name 只有 return 是一个空字符串。我期望它 return "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() 函数并取消 while 语句中代码的顺序的情况下检查以确保根节点的名称是 "TimeLog"?
尝试执行下面的代码,如果仍然有错误请告诉我:
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();
ReadStartElement
方法的说明:
Checks that the current node is an element and advances the reader to
the next node.
下一个节点是 XmlNodeType.Whitespace
.
考虑以下 XML:
<TimeLog>
<Entry>
也可以写成:
<TimeLog> <Entry>
两个XmlNodeType.Element
之间有XmlNodeType.Whitespace
。
MoveToContent
将 reader 设置为根节点 - TimeLog。然后ReadStartElement
读取TimeLog并移动到白色space.
以下视图中没有space:
<TimeLog><Entry>
顺便说一句,你可以使用 ReadToFollowing
方法:
using (var reader = XmlReader.Create("file.xml"))
{
reader.ReadToFollowing("TimeLog");
// rest of code
}
如果可以使用 LINQ 到 XML,请检查 XDocument.Root.Name
:
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);
}
根 元素 可以使用以下代码快速找到。通过使用 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 文档中的根 节点 是一个 XML 声明,这可能不是您想要的。
我有一个 XML 文件要加载。我想在我的代码中添加一个条件来检查并确保根节点是 'TimeLog'。如果不是,则告诉用户他们选择了错误的 XML 文件。
在下面的代码中,r.Name 只有 return 是一个空字符串。我期望它 return "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() 函数并取消 while 语句中代码的顺序的情况下检查以确保根节点的名称是 "TimeLog"?
尝试执行下面的代码,如果仍然有错误请告诉我:
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();
ReadStartElement
方法的说明:
Checks that the current node is an element and advances the reader to the next node.
下一个节点是 XmlNodeType.Whitespace
.
考虑以下 XML:
<TimeLog>
<Entry>
也可以写成:
<TimeLog> <Entry>
两个XmlNodeType.Element
之间有XmlNodeType.Whitespace
。
MoveToContent
将 reader 设置为根节点 - TimeLog。然后ReadStartElement
读取TimeLog并移动到白色space.
以下视图中没有space:
<TimeLog><Entry>
顺便说一句,你可以使用 ReadToFollowing
方法:
using (var reader = XmlReader.Create("file.xml"))
{
reader.ReadToFollowing("TimeLog");
// rest of code
}
如果可以使用 LINQ 到 XML,请检查 XDocument.Root.Name
:
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);
}
根 元素 可以使用以下代码快速找到。通过使用 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 文档中的根 节点 是一个 XML 声明,这可能不是您想要的。