从给出xml文件中读取xml节点 [英] Read xml node from give xml file

查看:104
本文介绍了从给出xml文件中读取xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,它不是格式良好的XML,因此如下所示



< category> 
< North>
< city>
< metro>
< details>
< citname>
< cityid>
< / cityid>
< pincode>
< / pincode>
< / citname>
< / details>
< / metro>
< / city>
< / North>
< / category>





所以我读了类别和细节之间的节点节点。节点值会改变它们只是动态类别,所有xml文件中的细节都保持不变。

所以我已经阅读了xml文件并获得了catagory和详细信息之间的标记值。



并且也只阅读城市名称等详细信息。





所以请帮助我。谢谢你的帮助。它在Visual Studio中开发的Web应用程序。



示例XML



 <?  xml    版本  =  1.0   编码  =  UTF- 8  >  

- < 类别 >


- < NORTH >


- < DELH我 >


- < METRO >


- < DETAILS >


- < CITYS >


- < CITYNAME >

< NAME > newtest < / NAME < span class =code-keyword>>

< 状态 > NW < / STATUS >

< SHORT_CODE > 12345 < / SHORT_CODE > ;

< / CITYNAME >


- < 帐户 >

< 允许 > < / ALLOW >


- < <跨度class =code-leadattribute> ACC_NEW >

< 有效性 > 14 < / VALIDITY >

< / ACC_NEW >


- < ACC_OLD >

< 有效性 > 20 < / VALIDITY >

< / ACC_OLD >


- < ACC_OTH >

< 有效性 >

< < span class =code-leadattribute> / ACC_OTH >

< ; / ACCOUNT >


- < MID >


- < MID_PROP >

< ID / >

< 有效性 > TRUE < / VALIDITY >

< / MID_PROP >

< / MID >


- < RENEWAL >

< RENEWAL > < / RENEWAL >

< / RENEWAL >


- < DIFFLOC >

< DIFF_PARAMAS > BANJ < / DIFF_PARAMAS >

< / DIFFLOC >


- < BILLING > ;

< CHARGE_ID > 12345 < / CHARGE_ID >

< / BILLING < span class =code-keyword>>


- < NEWLIST >

< < span class =code-leadattribute> DO_LIST > < / DO_LIST >

< / NEWLIST >


- < 消息 >

< MESSAGE > 你好欢迎< / MESSAGE >

< / MESSAGES >

< / CITYS >

< / DETAILS >

< / METRO >

< / DELHI >

< / NORTH >

< /类别 >





我有什么试过:



XmlNodeList xmlnodelist = xmlDoc.GetElementsByTagName(details);



但它没有用。

解决方案

我不确定你想要实现什么,但我强烈建议你阅读: XMLDocument vs XDocument vs XmlReader vs LINQ to Xml [ ^ ]和这: c# - XDocument或XmlDocument - Stack Overflow [ ^ ]



之一最简单的方法得到节点的名称及其值是使用 XDocument class + Linq

 XDocument xdoc = XDocument.Load(rdr); 

var nodes = xdoc.Descendants()。选择(x => x.Name);
// 返回IEnumerable< string>:
// category
//
// city
< span class =code-comment> // metro
// 详情
// citname
// cityid
/ / pincode





试试!


在我对你的第二个XML示例进行了一些清理之后...

问题是你正在使用索引进入 xmlnodelist (speci fically i )作为 xnod.ChildNodes 的索引。它们彼此无关。

而是尝试在 xmlnodelist 上使用 foreach

  //   for(int i = 0; i< xmlnodelist.Count; i ++) 
foreach var citysNode < span class =code-keyword> in xmlnodelist.Cast< XmlNode>())
{
// 使用 xmlnodelist中的每个节点执行某些操作
}



因为您似乎只想要集合的最后一个节点,所以只需要那个节点。不需要循环:

  if (xmlnodelist.Any())
{
LIST.Items.Add((XmlNode)(xmlnodelist.Last())。Name);
}



取决于:使用System.Linq;


< blockquote>你不可能通过一个简单的原因来处理格式不正确的XML:它根本不是XML。



你的第一个XML样本实际上是XML,结构良好但是你的第二个不是。



当然,你可以在不使用任何现有和正确的XML解析器的情况下阅读,但这只是浪费时间。垃圾进垃圾出。仅适用于格式良好的XML。如果某个文件不是这样的话,至少不要把它称为XML。



.NET FCL提供了不同的解析XML的方法。这是我对它们的简短概述:

  1. 使用 System.Xml.XmlDocument 类。它实现了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




  4. -SA


    HI I have a XML file and it is not well formed XML so it as following

    <category>
    <North>
    <city>
    <metro>
    <details>
    <citname>
    <cityid>
    </cityid>
    <pincode>
    </pincode>
    </citname>
    </details>
    </metro>
    </city>
    </North>
    </category>



    So i have read the node nodes between the category and details.the node value will change they are dynamic only category and details remain same in all the xml files.
    so i have read xml file and get the tag value between catagory and details.

    And also read the city details like city names only.


    so please help me.Thanks for your help guys. its web application developed in visual studio.

    SAMPLE XML

    <?xml version="1.0" encoding="UTF-8"?>
    
    -<category>
    
    
    -<NORTH>
    
    
    -<DELHI>
    
    
    -<METRO>
    
    
    -<DETAILS>
    
    
    -<CITYS>
    
    
    -<CITYNAME>
    
    <NAME>newtest</NAME>
    
    <STATUS>NW</STATUS>
    
    <SHORT_CODE>12345</SHORT_CODE>
    
    </CITYNAME>
    
    
    -<ACCOUNT>
    
    <ALLOW>YES</ALLOW>
    
    
    -<ACC_NEW>
    
    <VALIDITY>14</VALIDITY>
    
    </ACC_NEW>
    
    
    -<ACC_OLD>
    
    <VALIDITY>20</VALIDITY>
    
    </ACC_OLD>
    
    
    -<ACC_OTH>
    
    <VALIDITY/>
    
    </ACC_OTH>
    
    </ACCOUNT>
    
    
    -<MID>
    
    
    -<MID_PROP>
    
    <ID/>
    
    <VALIDITY>TRUE</VALIDITY>
    
    </MID_PROP>
    
    </MID>
    
    
    -<RENEWAL>
    
    <RENEWAL>YES</RENEWAL>
    
    </RENEWAL>
    
    
    -<DIFFLOC>
    
    <DIFF_PARAMAS>BANJ</DIFF_PARAMAS>
    
    </DIFFLOC>
    
    
    -<BILLING>
    
    <CHARGE_ID>12345</CHARGE_ID>
    
    </BILLING>
    
    
    -<NEWLIST>
    
    <DO_LIST>NO</DO_LIST>
    
    </NEWLIST>
    
    
    -<MESSAGES>
    
    <MESSAGE>HELLO WELCOME</MESSAGE>
    
    </MESSAGES>
    
    </CITYS>
    
    </DETAILS>
    
    </METRO>
    
    </DELHI>
    
    </NORTH>
    
    </category>



    What I have tried:

    XmlNodeList xmlnodelist = xmlDoc.GetElementsByTagName("details");

    but its not working.

    解决方案

    I'm not sure what are you trying to achieve, but i'd strongly recommend to read this: XMLDocument vs XDocument vs XmlReader vs LINQ to Xml[^] and this: c# - XDocument or XmlDocument - Stack Overflow[^]

    One of the easiest way to get the names of nodes and their values is to use XDocument class + Linq:

    XDocument xdoc = XDocument.Load(rdr);
    
    var nodes = xdoc.Descendants().Select(x=>x.Name);
    //returns IEnumerable<string>:
    //category 
    //North 
    //city 
    //metro 
    //details 
    //citname 
    //cityid 
    //pincode



    Try!


    After I did a little cleaning up of your second XML example...
    The problem is that you're using the index into xmlnodelist (specifically i) as the index into xnod.ChildNodes. They have nothing to do with each other.
    Instead try using foreach on the xmlnodelist:

    //for (int i = 0; i < xmlnodelist.Count; i++)
    foreach (var citysNode in xmlnodelist.Cast<XmlNode>())
    {
      // Do something with every node in xmlnodelist
    }
    


    Since you seem to only want the last node of the collection, then just get that one. No looping required:

    if (xmlnodelist.Any())
    {
      LIST.Items.Add((XmlNode)(xmlnodelist.Last()).Name);
    }


    Depends on: using System.Linq;


    You cannot possible work with not well-formed XML by one simple reason: it's not XML at all.

    Your first XML sample is really XML, well-formed, but your second one is not.

    Of course, you can read something without using any existing and correct XML parsers, but it would be just a waste of time. Garbage in, garbage out. Work only with well-formed XML. If some file is not such a thing, at least don't call it "XML".

    .NET FCL offers different ways of parsing 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.



    —SA


    这篇关于从给出xml文件中读取xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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