如何读取 C# 示例的节点内的文本?
这是我想要读取的 XML。
<Server ServerName="SP-SWD-T01">
Some nodes are there
</Server>
我想读取服务器内的服务器名称,如何读取它。请帮忙。
这是代码
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = false;
XmlReader xmlRdr = XmlReader.Create(strFilePath, readerSettings);
// Parse the file
while (xmlRdr.Read())
{
switch (xmlRdr.NodeType)
{
case XmlNodeType.Element:
// You may need to capture the last element to provide a context
// for any comments you come across... so copy xmlRdr.Name, etc.
break;
case XmlNodeType.Comment:
MessageBox.Show(xmlRdr.Name);
break;
case XmlNodeType.Text: //Display the text in each element.
//Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
//Console.Write("</" + reader.Name);
//Console.WriteLine(">");
break;
}
}
谢谢
This is the XML that i want to read.
<Server ServerName="SP-SWD-T01">
Some nodes are there
</Server>
I want to read the ServerName inside the server how can i read it.Please help.
This is the code
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = false;
XmlReader xmlRdr = XmlReader.Create(strFilePath, readerSettings);
// Parse the file
while (xmlRdr.Read())
{
switch (xmlRdr.NodeType)
{
case XmlNodeType.Element:
// You may need to capture the last element to provide a context
// for any comments you come across... so copy xmlRdr.Name, etc.
break;
case XmlNodeType.Comment:
MessageBox.Show(xmlRdr.Name);
break;
case XmlNodeType.Text: //Display the text in each element.
//Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
//Console.Write("</" + reader.Name);
//Console.WriteLine(">");
break;
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
try this