我们如何在C语言中进行XML解析 [英] how we do XML parsing in C sharp
本文介绍了我们如何在C语言中进行XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我找到XML节点.
我的XML文件是:
please help me find node in XML.
my XML file is:
<?xml version="1.0" encoding="UTF-8"?>
<DistanceMatrixResponse>
<status>OK</status>
<origin_address>California, USA</origin_address>
<destination_address>Louisiana, USA</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>110693</value>
<text>1 day 7 hours</text>
</duration>
<distance>
<value>3037646</value>
<text>3,038 km</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>
我想要与该XML文件的距离.我正在使用此代码,但出现错误
I want distance from this XML file. I''m using this code but it gives error
XmlDocument xml = new XmlDocument();
xml.LoadXml(tempString); // suppose that str string contains "<Names>...</Names>"
// XmlNodeList xnList = xml.SelectNodes("/DistanceMatrixResponse/text/[@type='distance']");
XmlNode node = xml.SelectSingleNode("DistanceMatrixResponse/row/element/distance/text()");
foreach (XmlNode xn in node)
{
Console.WriteLine(xn.InnerText);
}
推荐答案
您的意思是:
Do you mean:
XmlDocument xml = new XmlDocument();
xml.Load("myfile.xml");
XmlNode node;
node = xml.SelectSingleNode("DistanceMatrixResponse/row/element/distance/text");
Console.WriteLine(node.InnerText);
?
使用以下代码读取Xml文件-
Use following code to read Xml file-
Now add namespace - using System.Xml;
on button click write code as-
XmlTextReader reader = new XmlTextReader("C:\\Users\\Dell\\Desktop\\New folder\\XMLFile1.xml"); //this will location and name of xml
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
MessageBox.Show("<" + reader.Name);
//MessageBox.Show(">");
//MessageBox.Show(reader.Value);
break;
case XmlNodeType.Text: //Display the text in each element.
MessageBox.Show(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
//Console.Write("</" + reader.Name);
//MessageBox.Show(">");
break;
}
}
.NET Framework随附了用于解析XML的不同库类;请在下面查看我的概述:
The .NET Framework comes with different library classes used to parse XML; please see my overview of them below:
- 使用
System.Xml.XmlDocument
类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
请参见- 使用类
].System.Xml.XmlTextReader
; library/system.xml.xmldocument.aspx"target =" _ blank"title =" New Window> ^ - 使用类
- 使用类
System.Xml.XmlTextReader
;这是最快的读取方法,尤其是您需要跳过一些数据.
请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [
- Use
System.Xml.XmlDocument
class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^]. - Use the class
System.Xml.XmlTextReader
; this is the fastest way of reading, especially is you need to skip some data.
See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^]. - Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, supporting LINQ to XML Programming.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].
这篇关于我们如何在C语言中进行XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文