读取具有多个节点的xml文档

时间:2015-10-24 14:27:22

标签: c# xml xmldocument

之前我使用过以下代码,但这次我的xml不同了:

protected string ReturnXmlValue(XmlDocument myXDoc, string field)
{
  var retval = string.Empty;


  try
  {
    var node = myXDoc.GetElementsByTagName(field);
    if (node.Count > 0)
    {
      var xmlNode = node.Item(0);
      if (xmlNode != null)
      {
        retval = xmlNode.InnerText;
      }
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.ToString());
    throw;
  }

  return retval;
}

以下是我的xml文件的一个示例:dummied down:

<RichDBDS>
  <TrxDetailCard>
    <TRX_HD_Key>18683435</TRX_HD_Key>
    <Date_DT>2015-10-22T21:32:00.233+00:00</Date_DT>
    <TRX_Card_Key>15263569</TRX_Card_Key>
    <Total_Amt_MN>22.0000</Total_Amt_MN>
    <Result_CH>0    </Result_CH>
    <Result_Txt_VC>APPROVED</Result_Txt_VC>
    <Approval_Code_CH>0943253</Approval_Code_CH>
  </TrxDetailCard>
  <TrxDetailCard>
    <TRX_HD_Key>18683825</TRX_HD_Key>
    <Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>
    <TRX_Card_Key>15263569</TRX_Card_Key>
    <Total_Amt_MN>32.0000</Total_Amt_MN>
    <Result_CH>0    </Result_CH>
    <Result_Txt_VC>APPROVED</Result_Txt_VC>
    <Approval_Code_CH>093389</Approval_Code_CH>
  </TrxDetailCard>
</RichDBDS>

我没有使用xml,所以我不确定如何搜索特定数量的内容。我可以有几个TrxDetailCards。我知道当我只有一个TrxDetailCard时如何获得金额,但是我需要返回TrxDetailCard以获得我需要的金额。

因此,如果我正在寻找32.00的TrxDetailCard,我需要返回的方法:

  <TrxDetailCard>
    <TRX_HD_Key>18683825</TRX_HD_Key>
    <Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>
    <TRX_Card_Key>15263569</TRX_Card_Key>
    <Total_Amt_MN>32.0000</Total_Amt_MN>
    <Result_CH>0    </Result_CH>
    <Result_Txt_VC>APPROVED</Result_Txt_VC>
    <Approval_Code_CH>093389</Approval_Code_CH>
  </TrxDetailCard>

我将如何做到这一点?

2 个答案:

答案 0 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<RichDBDS>" +
                  "<TrxDetailCard>" +
                    "<TRX_HD_Key>18683435</TRX_HD_Key>" +
                    "<Date_DT>2015-10-22T21:32:00.233+00:00</Date_DT>" +
                    "<TRX_Card_Key>15263569</TRX_Card_Key>" +
                    "<Total_Amt_MN>22.0000</Total_Amt_MN>" +
                    "<Result_CH>0    </Result_CH>" +
                    "<Result_Txt_VC>APPROVED</Result_Txt_VC>" +
                    "<Approval_Code_CH>0943253</Approval_Code_CH>" +
                  "</TrxDetailCard>" +
                  "<TrxDetailCard>" +
                    "<TRX_HD_Key>18683825</TRX_HD_Key>" +
                    "<Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>" +
                    "<TRX_Card_Key>15263569</TRX_Card_Key>" +
                    "<Total_Amt_MN>32.0000</Total_Amt_MN>" +
                    "<Result_CH>0    </Result_CH>" +
                    "<Result_Txt_VC>APPROVED</Result_Txt_VC>" +
                    "<Approval_Code_CH>093389</Approval_Code_CH>" +
                  "</TrxDetailCard>" +
                "</RichDBDS>";

            XElement richDBDS = XElement.Parse(xml);
            XElement results = richDBDS.Elements("TrxDetailCard").Where(x => (decimal)x.Element("Total_Amt_MN") == (decimal)32.0000).FirstOrDefault();
 
        }
    }
}
​

答案 1 :(得分:0)

您可以使用Linq-to-Xml

var str = File.ReadAllText(@"C:\YourDirectory\sample.xml");
XDocument xDoc = XDocument.Parse(str);

var trxNodes = xDoc.Descendants("TrxDetailCard");
var node = trxNodes.First(n => double.Parse(n.Element("Total_Amt_MN").Value) == 32.00);