I have used the following code before but my xml is different this time:
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;
}
Here is an example of my xml file dummied down a bit:
<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>
I've not worked with xml much so I'm not sure how to search this for a specific amount. I can have several TrxDetailCards. I know how to get amount when I only have one TrxDetailCard but I need to return the TrxDetailCard for the hits on the amount that I need.
So if I am looking for the TrxDetailCard that is 32.00, I need the method to return:
<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>
How would I go about doing this?
Try this
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(); } } }
You can use 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.