从Xml文件中提取值
本文关键字:提取 文件 Xml | 更新日期: 2023-09-27 18:22:02
我正试图从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"中提取值
如何迭代节点以提取它们?
谢谢。
首先,使用较新的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);
您可以使用LINQ到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;
您也可以尝试
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);
}
}
如果你真的必须使用旧的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);
}