阅读XML标签时出现问题 [英] Problem in reading XML tags
本文介绍了阅读XML标签时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从以下XML中读取HeadId和Amount的值:
How to read the values of HeadId and Amount from the following XML:
<PARAMS>
<BasicSetup HeadId="10" Amount="122" />
<BasicSetup HeadId="11" Amount="222" />
<BasicSetup HeadId="12" Amount="333" />
<BasicSetup HeadId="13" Amount="444" />
</PARAMS>
推荐答案
您好,
阅读XML : http://csharp.net-tutorials.com/xml/reading- xml-with-the-xmlreader-class / [ ^ ]
使用Linq读取XML: http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/reading-xml-file-通过-linq-a-few-tips / [ ^ ]
Hi,
Reading XML : http://csharp.net-tutorials.com/xml/reading-xml-with-the-xmlreader-class/[^]
Reading XML using Linq : http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/reading-xml-file-through-linq-a-few-tips/[^]
.NET FCL提供了与XML解析相关的不同内容。这是我对他们的简短概述:
Different approached to XML parsing are offered by .NET FCL. This is my short overview of them:
- 使用
System.Xml.XmlDocument
class。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。 - 使用类
System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。 - 使用类
System.Xml.Linq.XDocument
;这是类似于XmlDocument
的最合适的方式,支持LINQ to XML Programming。
参见 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[^].
这里是简单的解决方案创建类BasicSetup
here is simple solution create class BasicSetup
class BasicSetup
{
public string HeadId { get; set; }
public string Amount { get; set; }
}
这里是读取xml对象的代码< br $>
and here is code to read xml object
string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\XMLData.xml";
XDocument objXML = XDocument.Load(path);
List<BasicSetup> objPARAMS = (from b in objXML.Descendants("BasicSetup")
select new BasicSetup()
{
HeadId = b.Attribute("HeadId").Value,
Amount = b.Attribute("Amount").Value
}).ToList<BasicSetup>();
foreach(BasicSetup obj in objPARAMS )
{
Console.WriteLine(String.Format("HeadId : {0} and Amount : {1}.", obj.HeadId,obj.Amount));
}
这篇关于阅读XML标签时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文