简体   繁体   中英

Fetching record from XML on the base of condition using Linq or any other way in .Net

I'm new to LINQ and trying to fetch record from the XML document using LINQ so please help me with the Below scenario:

XML Document:

<ParsedResultSet>  
 <Orders_Renewals>
    <file_line_number>1</file_line_number>
    <issn>15338606</issn>
    <publisher_title_reference>ABC</publisher_title_reference>
    <journal_title>Music</journal_title>
    <publisher_subscription_reference>54562</publisher_subscription_reference>
    <agent_subscription_reference>86031</agent_subscription_reference>
  </Orders_Renewals>
  <Orders_Renewals>
    <file_line_number>2</file_line_number>
    <issn>15338606</issn>
    <publisher_title_reference></publisher_title_reference>
    <journal_title>Music1</journal_title>
    <publisher_subscription_reference>598782</publisher_subscription_reference>
    <agent_subscription_reference>86276</agent_subscription_reference>
  </Orders_Renewals>
  <Orders_Renewals>
    <file_line_number>3</file_line_number>
    <issn>15338606</issn>
    <publisher_title_reference>DEF</publisher_title_reference>
    <journal_title>Music2</journal_title>
    <publisher_subscription_reference>507682</publisher_subscription_reference>
    <agent_subscription_reference>31276</agent_subscription_reference>
  </Orders_Renewals>
</ParsedResultSet>

My searching condition like:

Select publisher_title_reference from Orders_Renewals where publisher_subscription_reference = '598782'and agent_subscription_reference = '31276'

Result Should be : XYZ

I have tried with below code but I'm not getting any ouput:

string xmlPath = @"D:\Temp\FileState" + "_" + xmlFileID;
                XDocument xml = XDocument.Load(xmlPath);
                var XMLOrderCodeNode = from n in xml.Elements("Orders_Renewals")
                                       where n.Attribute("agent_subscription_reference").Value.ToString().Trim() == agentRefNbr
                                       select n.Attribute("publisher_title_reference").Value.ToString().Trim();

Please help me with the scenario and Thank you in advance.

agent_subscription_reference and publisher_title_reference are not attributes. Use this query to get the Descendants and then find the elements:

var XMLOrderCodeNode = from n in xml.Descendants("Orders_Renewals")
                        where n.Element("agent_subscription_reference").Value.ToString().Trim() == agentRefNbr
                        select n.Element("publisher_title_reference").Value.ToString().Trim();

When agent_subscription_reference is equal to 31276 , it should return DEF not XYZ as you have mentioned.

Try code below. You can add a where to filter as required :

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var orders = doc.Descendants("Orders_Renewals").Select(x => new {
                file_line_number = (int)x.Element("file_line_number"),
                issn = (string)x.Element("issn"),
                reference = (string)x.Element("publisher_title_reference"),
                title = (string)x.Element("journal_title"),
                subscription = (string)x.Element("publisher_subscription_reference"),
                agent = (int)x.Element("agent_subscription_reference")
            }).ToList();

        int agentRefNbr = 86031;
        var results = orders.Where(x => x.agent == agentRefNbr).ToList();



        }
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM