如何根据C#中的某些条件从XML文件中读取数据? [英] How to Read data from a XML file based on some condition in C#?

查看:233
本文介绍了如何根据C#中的某些条件从XML文件中读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据C#中的某些条件从xml文件中读取数据?



请建议我.....

How to read data from a xml file based on some condition in C#?

Please Suggest Me.....

推荐答案




要从xml文件中读取数据,我首先将xml序列化为强类型对象。



查看此



从-XML到强类型在C





基本上使用类可以将xml文件序列化为对象,然后在c#中正常使用。



这有用吗?
Hi
To read data from xml file I would first serialize the xml to a strongly typed object.

Check this

From-XML-to-Strong-Types-in-C


Basically using a class you can serialize the xml file to an object and then use this as normal in your c#.

Does this help?


您没有根据您想要读取数据的条件提及

使用linq您可以这样做。



检查这些链接可能会对你有所帮助。





http://stackoverflow.com/questions/1549908 2 / linq-to-xml-fetching-value-based-on-condition [ ^ ]



http://www.c-sharpcorner.com/UploadFile/54db21/ select-data-from-xml-with-where-clause-using-linq-to-xml / [ ^ ]
You didn't mention based on what condition you want to read data but
Using linq you can do this.

Check these link it may help you.


http://stackoverflow.com/questions/15499082/linq-to-xml-fetching-value-based-on-condition[^]

http://www.c-sharpcorner.com/UploadFile/54db21/select-data-from-xml-with-where-clause-using-linq-to-xml/[^]


你也可以在LINQ中使用类XDocument,XElement,XAttribute等。

这种方法不是强类型的,但你必须手动阅读结构。



System.Xml.Linq命名空间 [ ^ ]



小例子:

You can also use the classes XDocument, XElement, XAttribute etc. in LINQ.
This approach is not strongly typed, but you have to read the structure manually.

System.Xml.Linq Namespace[^]

Small example:
XDocument xd = new XDocument();
xd.Load(@"SomeFile.xml");

// Get the first element with the name 'SomeNode'
XElement xe = xd.Element("SomeNode");
etc.





如果您只想读取数据,这是XSD的一个很好的替代方案。



This is a good alternative to XSD if you only want to read the data.


这篇关于如何根据C#中的某些条件从XML文件中读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆