从Xml文件中提取值

时间:2013-07-11 14:20:11

标签: c# xml

我试图从xml文件中提取以下值: - - xml文件表示如下:

 <ENVELOPE_CONTENT>
   <DOCUMENTS>
    <DOCUMENT>
      <IDX>1529</IDX> 
      <ENTITY_PRIORITY>5</ENTITY_PRIORITY> 
      <CLD_COD>MAGAZINE</CLD_COD> 
      <CLD_DESC>Revues, magazine</CLD_DESC> 
      <CATEGORY>OTHER</CATEGORY> 
      <TIF_FILENAME>revues, magazine_1529_si.tif</TIF_FILENAME> 
      <COMMENT /> 
      <REJECT_MESSAGES /> 
      <PAGES>
       <PAGE>
         <PAGIDX>3375</PAGIDX> 
         <POSITION>1</POSITION> 
         <TIFNAME>87771593-2FD4-4803-8736-E2C1A898A96B_002.tif</TIFNAME> 
         <JPEGNAME>87771593-2fd4-4803-8736-e2c1a898a96b_001.jpg</JPEGNAME> 
       </PAGE>
       <PAGE>
        <PAGIDX>3376</PAGIDX> 
        <POSITION>2</POSITION> 
        <TIFNAME>87771593-2FD4-4803-8736-E2C1A898A96B_004.tif</TIFNAME> 
        <JPEGNAME>87771593-2fd4-4803-8736-e2c1a898a96b_003.jpg</JPEGNAME> 
      </PAGE>
      <PAGE>
        <PAGIDX>3377</PAGIDX> 
        <POSITION>3</POSITION> 
        <TIFNAME>87771593-2FD4-4803-8736-E2C1A898A96B_006.tif</TIFNAME> 
        <JPEGNAME>87771593-2fd4-4803-8736-e2c1a898a96b_005.jpg</JPEGNAME> 
      </PAGE>
      <PAGE>
        <PAGIDX>3378</PAGIDX> 
        <POSITION>4</POSITION> 
        <TIFNAME>87771593-2FD4-4803-8736-E2C1A898A96B_008.tif</TIFNAME> 
        <JPEGNAME>87771593-2fd4-4803-8736-e2c1a898a96b_007.jpg</JPEGNAME> 
      </PAGE>
      </PAGES>
     </DOCUMENT>
    </DOCUMENTS>  </ENVELOPE_CONTENT>

我使用以下c#代码来提取值

string xmlText = File.ReadAllText(f);
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlText);
XmlNodeList parentNode = doc.GetElementsByTagName("DOCUMENT");

List<string> p = new List<string>();
string classe = "";
foreach (XmlNode childrenNode in parentNode)
{
     classe = childrenNode.SelectSingleNode("CLD_COD").InnerText;

 }//end foreach

我设法从CLD_COD中提取值但是我无法设法提取“TIFNAME”中的值

如何迭代节点来提取它们?

谢谢。

4 个答案:

答案 0 :(得分:3)

首先,使用较新的XML API Linq-2-XML(XLinq)可以轻松实现这一点。

var root = XElement.Parse(xmlText);  // or directly .Load(fileName)

List<string> tifNames = root.Descendants("TIFNAME").Select(e => e.Value);

答案 1 :(得分:2)

您可以使用LINQ to XML:

XDocument xdoc = XDocument.Load(f);
var cldCod = (string)xdoc.Descendants("CLD_COD").FirstOrDefault();
var names = from p in xdoc.Descendants("PAGE")
            select (string)p.Element("TIFNAME");

另一种选择是XPath扩展。您可以指定元素的确切路径以避免整个xml查找:

var root = xdoc.Root;
var cldCod = (string)root.XPathSelectElement("DOCUMENTS/DOCUMENT/CLD_COD");
var names = from n in root.XPathSelectElements("DOCUMENTS/DOCUMENT/PAGES/PAGE/TIFNAME")
            select (string)n;

答案 2 :(得分:2)

您也可以尝试这个

        string xmlText = File.ReadAllText("C:\\Users\\virens\\Desktop\\Testxml.xml");
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlText);
        XmlNodeList parentNode = doc.GetElementsByTagName("DOCUMENT");
        IEnumerator testnodes = parentNode[0].ChildNodes.GetEnumerator();

        List<string> p = new List<string>();
        string classe = "";

        while (testnodes.MoveNext())
        {

            XmlNode node = (XmlNode)testnodes.Current;

            if (node.Name == "TIF_FILENAME")
            {
                Console.WriteLine("Yai I got it");
                Console.WriteLine(node.InnerText);
            }
        } 

答案 3 :(得分:1)

如果你真的必须使用较旧的XmlDocument,你可以尝试这样的事情:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlText);
XmlNodeList xn = doc.SelectNodes("/ENVELOPE_CONTENT/DOCUMENTS/DOCUMENT/PAGES/PAGE/TIFNAME");

foreach (XmlNode xnode in xn)
{                   
    //extract values here
    Console.WriteLine(xnode.InnerText);
}