如何从Xml文件C#的元素中提取值? [英] How Do I Extract The Value From The Element Of A Xml File C#?

查看:108
本文介绍了如何从Xml文件C#的元素中提取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何从这个xml中提取数据值...



 <   root  >  
< FileTransfer i = 1 ad = abc >
< pot > 250 < / pot >
< <跨度class =code-keyword>> abc < / prod >
< des > yoko < / des >
< / FileTransfer < span class =code-keyword>>
< island >
< name > michael < / name >
< 数据 > 美国< / data >
< / island >
< < span class =code-leadattribute> / root >





请帮助!!!

解决方案

阅读标准MSDN文档有什么问题? .NET提供了解析XML的不同方法。这是我对他们的简短概述:



  1. 使用 System.Xml.XmlDocument class 。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类 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 [ ^ ]。





您不必在XML级别使用解析。有可能,您可以使用XML序列化序列化和反序列化所需的数据结构。最好的方法是数据合同 http://msdn.microsoft .com / zh-CN / library / ms733127.aspx [ ^ ]。



祝你好运,

-SA

  var  document = XDocument.Load( @  D:\ test.xml); 
var nodes = document.Descendants()。其中​​(e = > e.Name .LocalName.StartsWith( data));
var values = nodes.Select(n = > n.Value).ToList( );





如果XML是这样的话

 <  产品 >  
< span class =code-keyword>< island >
< 名称 > AAA < / name >
< 数据 > sfsgsdg < / data >
< / island >
< 孤岛 >
< 名称 > BBB < / name >
< 数据 > sgtyeyewy < / data >
< / island >
< span class =code-keyword>< island >
< name > CCC < / name >
< data > rhjfjgfjkgfj < / data >
< / island >
< / products >





变量values包含值列表,例如{sfsgsdg, sgtyeyewy,rhjfjgfjkgfj}


使用此XML库: http://elmax.codeplex.com / [ ^ ]。注意:这个库有两种语言版本:C ++和C#



使用Elmax; 

元素root = new Element();
root.SetDomDoc(doc); //事先将XML文件读入DOM文档。
元素elemData = root [root | island | data];
if(elemData.Exists)
{
string data = elemData.GetString(empty);
}





我忘了展示如何加载XML。这是来自 http://elmax.codeplex.com/wikipage?title= CSharp%20Elmax& amp; referTitle = Documentation#LoadXMLDocument [ ^ ]。调用 CreateAndLoadXml 后,将 doc 传递给 SetDomDoc 方法。



  bool  CreateAndLoadXml( out  XmlDocument doc,System。 String  strFilename)
{
doc = new XmlDocument();
尝试
{
doc.Load(strFilename);
}
catch (System.Exception)
{
return false ;
}
返回 true ;
}


Hi,
how can i extract the "data" value form this xml ...

<root>
<FileTransfer i="1" ad="abc">
<pot>250</pot>
<prod>abc</prod>
<des>yoko</des>
</FileTransfer>
<island>
<name>michael</name>
<data>america</data>
</island>
</root>



Please help!!!

解决方案

What's wrong with reading standard MSDN documentation? .NET offers different ways to parse XML. This is my short overview of them:

  1. 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[^].
  2. 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[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, 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[^].



You don't have to use parsing at XML level. Chances are, you can just serialize and deserialize the data structures you need using XML serialization. The best way to do it is Data Contract: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Good luck,

—SA


var document = XDocument.Load(@"D:\test.xml");
          var nodes = document.Descendants().Where(e => e.Name.LocalName.StartsWith("data"));
          var values = nodes.Select(n => n.Value).ToList();



if XML is like this

<products>
  <island>
    <name>AAA</name>
    <data>sfsgsdg</data>
  </island>
  <island>
    <name>BBB</name>
    <data>sgtyeyewy</data>
  </island>
  <island>
    <name>CCC</name>
    <data>rhjfjgfjkgfj</data>
  </island>
</products>



the variable "values" contains list of values e.g {"sfsgsdg","sgtyeyewy","rhjfjgfjkgfj"}


Use this XML library: http://elmax.codeplex.com/[^]. Note: this library comes in 2 language flavors: C++ and C#

using Elmax;

Element root = new Element();
root.SetDomDoc(doc); // A XML file is read into the DOM doc beforehand.
Element elemData = root["root|island|data"];
if(elemData.Exists)
{
    string data = elemData.GetString("empty");
}



I forgot to show how to load the XML. Here it is from http://elmax.codeplex.com/wikipage?title=CSharp%20Elmax&amp;referringTitle=Documentation#LoadXMLDocument[^]. After calling CreateAndLoadXml, pass the doc to SetDomDoc method.

bool CreateAndLoadXml(out XmlDocument doc, System.String strFilename)
{
    doc = new XmlDocument();
    try
    {
        doc.Load(strFilename);
    }
    catch (System.Exception)
    {
        return false;
    }
    return true;
}


这篇关于如何从Xml文件C#的元素中提取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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