如何提取xml子元素

时间:2014-07-28 21:55:06

标签: c# xml

我试图找出提取xml子代码的代码(我认为这是措辞正确)元素。我已经搜索并尝试了许多样本,但无法找到如何向下钻取以找出我想要的部分并返回我需要的信息。也许我所需要的只是有人来定义我想要的数据,所以我可以阅读这个问题,当然任何代码都会非常有用,我会从那里弄明白。感谢先进的任何帮助!

这是xml文件。我正在尝试运行if语句来查找名为<STATISTICTYPE>PVCAP_CharactersSaved</STATISTICTYPE>的部分并返回<JOBNAME><TIMEDELTA><VALUESUM>

<?xml version="1.0" encoding="utf-8"?>
<PVCAPTURESTATISTICCONTAINTER xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PVCAPTUREJOBSTATISTICS>
    <PVCAPTURESTATISTICSUMMARY>
      <STATISTICTYPE>PVCAP_CharactersSaved</STATISTICTYPE>
      <STATISTICNAME>Characters saved</STATISTICNAME>
      <JOBID>24</JOBID>
      <JOBNAME>HEAT FILES</JOBNAME>
      <TIMEDELTA>422</TIMEDELTA>
      <VALUESUM>25432</VALUESUM>
    </PVCAPTURESTATISTICSUMMARY>
    <PVCAPTURESTATISTICSUMMARY>
      <STATISTICTYPE>PVCAP_CharactersSaved_NoMM</STATISTICTYPE>
      <STATISTICNAME>Characters saved (no match and merge)</STATISTICNAME>
      <JOBID>24</JOBID>
      <JOBNAME>HEAT FILES</JOBNAME>
      <TIMEDELTA>422</TIMEDELTA>
      <VALUESUM>25432</VALUESUM>
    </PVCAPTURESTATISTICSUMMARY>
    </PVCAPTUREJOBSTATISTICS>
      <DOCUMENTCOUNT>762</DOCUMENTCOUNT>
      <PAGECOUNT>3194</PAGECOUNT>  
      <IMAGECOUNT>3194</IMAGECOUNT>  
      <VERSION>2.0</VERSION>
</PVCAPTURESTATISTICCONTAINTER>

3 个答案:

答案 0 :(得分:0)

您可以使用LINQ to XML,尤其是XElement类。

var element = XElement.Parse(xmlStr).Element("PVCAPTUREJOBSTATISTICS")
                                    .Elements("PVCAPTURESTATISTICSUMMARY")
                                    .First(c => c.Element("STATISTICTYPE").Value == "PVCAP_CharactersSaved")

var jobName = element.Element("JOBNAME").Value;
var timeDelta = element.Element("TIMEDELTA").Value;
var valueSum = element.Element("VALUESUM").Value;

你想在这里添加一些错误处理和诸如此类的东西,但这应该让你朝着正确的方向前进。

答案 1 :(得分:0)

您可以这样做:

XElement res = XElement.Parse(xmlResult);
foreach(var elem in res.Element("PVCAPTUREJOBSTATISTICS").Elements("PVCAPTURESTATISTICSUMMARY"))
{
    if (elem.Element("STATISTICTYPE").Value.Equals("PVCAP_CharactersSaved", StringComparison.Ordinal))
    {
        string jobName = elem.Element("JOBNAME").Value;
        string timeDelta = elem.Element("TIMEDELTA").Value;
        string valueSum = elem.Element("VALUESUM").Value;
    }
}

答案 2 :(得分:0)

您可以使用XDocument和LINQ-to-XML轻松完成此操作,例如:

string xml = "your xml content here";
XDocument doc = XDocument.Parse(xml);
//or if you have the xml file instead :
//XDocument doc = XDocument.Load("path_to_xml_file.xml");
var result = doc.Descendants("PVCAPTURESTATISTICSUMMARY")
                .Where(o => (string) o.Element("STATISTICTYPE") == "PVCAP_CharactersSaved")
                .Select(o => new
                                 {
                                     jobname = (string) o.Element("JOBNAME"),
                                     timedelta = (string) o.Element("TIMEDELTA"),
                                     valuesum = (string) o.Element("VALUESUM")
                                 });
foreach (var r in result)
{
    Console.WriteLine(r);
}