none
LINQ to XML: How to filter data and get filter xml as a string RRS feed

  • Question

  • This is my sample xml

    <?xml version="1.0" encoding="utf-8"?>
    <TickerBrokerStandardDateLineitem>
      <Ticker />
      <TickerID />
      <TickerBrokerStandardDateLineitemValues>
        <TickerBrokerStandardDateLineitemValue>
          <TabName>Consensus Model</TabName>
          <StandardDate>1Q 2010</StandardDate>
          <BRTab>Income Statement</BRTab>
          <BRLineItem>NET REVENUES</BRLineItem>
          <Action>Extracted</Action>
          <StandardLineItem>Net Revenue</StandardLineItem>
          <StandardValue>329.623</StandardValue>
        </TickerBrokerStandardDateLineitemValue>
        <TickerBrokerStandardDateLineitemValue>
          <TabName>Consensus Model</TabName>
          <StandardDate>2Q 2010</StandardDate>
          <BRTab>Income Statement</BRTab>
          <BRLineItem>NET REVENUES</BRLineItem>
          <Action>Extracted</Action>
          <StandardLineItem>Net Revenue</StandardLineItem>
          <StandardValue>454.776</StandardValue>
        </TickerBrokerStandardDateLineitemValue>
      </TickerBrokerStandardDateLineitemValues>
    </TickerBrokerStandardDateLineitem>

    i want to get only those records whose StandardValue is not empty or null. after apply this filter how can i get filter xml string ?

    i did it this way but could not get filtered xml

                            XDocument doc = XDocument.Load(brokerBogeypath);
                            var query = from r in doc.Descendants("TickerBrokerStandardDateLineitemValue")
                                        where !string.IsNullOrEmpty((string)r.Element("StandardValue"))
                                        select new
                                        {
                                            TabName = r.Element("TabName").Value,
                                            StandardDate = r.Element("StandardDate").Value,
                                            BRTab = r.Element("BRTab").Value,
                                            BRLineItem = r.Element("BRLineItem").Value,
                                            Action = r.Element("Action").Value,
                                            StandardLineItem = r.Element("StandardLineItem").Value,
                                            StandardValue = r.Element("StandardValue").Value,
                                        };
    doc.ToString() giving me whole xml not after applying filter. can i write query.tostring() ?  please help.



    • Edited by Sudip_inn Monday, July 29, 2019 4:10 PM
    Monday, July 29, 2019 4:09 PM

Answers

  • Hi Sudip_inn,

    Thanks for the feedback.

    According to your new description, I modified the code.

    string path = "D:\\test.xml"; XDocument doc = XDocument.Load(path);

    doc.Descendants("TickerBrokerStandardDateLineitemValue").Where(x => string.IsNullOrEmpty((string)x.Element("StandardValue"))).Remove() ; Console.WriteLine(doc.ToString());

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, July 31, 2019 10:59 AM
    Wednesday, July 31, 2019 3:29 AM
    Moderator

All replies

  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, you want to filter data and get filter xml as a string.

    I have modified your code, you could have a look.

                string path = "D:\\test.xml";
                XDocument doc = XDocument.Load(path);
                var query = from r in doc.Descendants("TickerBrokerStandardDateLineitemValue")
                            where !string.IsNullOrEmpty((string)r.Element("StandardValue"))
                            select new
                            {
                                TabName = r.Element("TabName").ToString(),
                                StandardDate = r.Element("StandardDate").ToString(),
                                BRTab = r.Element("BRTab").ToString(),
                                BRLineItem = r.Element("BRLineItem").ToString(),
                                Action = r.Element("Action").ToString(),
                                StandardLineItem = r.Element("StandardLineItem").ToString(),
                                StandardValue = r.Element("StandardValue").ToString(),
                            };
                foreach (var item in query)
                {
                    Console.WriteLine(item.TabName);
                    Console.WriteLine(item.StandardDate);
                    Console.WriteLine(item.BRTab);
                    Console.WriteLine(item.BRLineItem);
                    Console.WriteLine(item.Action);
                    Console.WriteLine(item.StandardLineItem);
                    Console.WriteLine(item.StandardValue);
                    Console.WriteLine("***********");
                }
                Console.ReadKey();

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, July 30, 2019 2:21 AM
    Moderator
  • thanks but i want to store the whole filter xml into a string variable.....how to do it ? if i write query.tostring() does it return the whole filtered xml ?
    Tuesday, July 30, 2019 7:21 AM
  • Hi Sudip_inn,

    Thanks for the feedback.

    If you want to store the whole filter xml into a string variable, you could use the following code.

                string path = "D:\\test.xml";
                XDocument doc = XDocument.Load(path);
                var query = from r in doc.Descendants("TickerBrokerStandardDateLineitemValue")
                            where !string.IsNullOrEmpty((string)r.Element("StandardValue"))
                            select new
                            {
                                TabName = r.Element("TabName").ToString(),
                                StandardDate = r.Element("StandardDate").ToString(),
                                BRTab = r.Element("BRTab").ToString(),
                                BRLineItem = r.Element("BRLineItem").ToString(),
                                Action = r.Element("Action").ToString(),
                                StandardLineItem = r.Element("StandardLineItem").ToString(),
                                StandardValue = r.Element("StandardValue").ToString(),
                            };
                string a = string.Empty;
                foreach (var item in query)
                {
                    a =a+item.TabName + item.StandardDate + item.BRTab + item.BRLineItem + item.Action + item.StandardLineItem + item.StandardValue;
    
                }
                Console.WriteLine(a);

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 30, 2019 7:37 AM
    Moderator
  • that is one way but would it be good to concatenate in loop. when we work with XDocument doc then we can use doc.ToString() to get whole xml but after query there is no way to get filter xml data without foreach ?

    if i iterate and concatenate in string the way you have shown here then these below element will be missing

    TickerBrokerStandardDateLineitem, TickerBrokerStandardDateLineitemValues & TickerBrokerStandardDateLineitemValue

    which is not desired.

    • Edited by Sudip_inn Tuesday, July 30, 2019 2:58 PM
    Tuesday, July 30, 2019 2:56 PM
  • Hi Sudip_inn,

    Thanks for the feedback.

    According to your new description, I modified the code.

    string path = "D:\\test.xml"; XDocument doc = XDocument.Load(path);

    doc.Descendants("TickerBrokerStandardDateLineitemValue").Where(x => string.IsNullOrEmpty((string)x.Element("StandardValue"))).Remove() ; Console.WriteLine(doc.ToString());

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, July 31, 2019 10:59 AM
    Wednesday, July 31, 2019 3:29 AM
    Moderator
  • Thanks it worked.

                string strXML = @"<?xml version=""1.0"" encoding=""utf-8""?>
                <TickerBrokerStandardDateLineitem>
                  <Ticker />
                  <TickerID />
                  <TickerBrokerStandardDateLineitemValues>
                    <TickerBrokerStandardDateLineitemValue>
                      <TabName>Consensus Model1</TabName>
                      <StandardDate>1Q 2010</StandardDate>
                      <BRTab>Income Statement</BRTab>
                      <BRLineItem>NET REVENUES</BRLineItem>
                      <Action>Extracted</Action>
                      <StandardLineItem>Net Revenue</StandardLineItem>
                      <StandardValue>329.623</StandardValue>
                    </TickerBrokerStandardDateLineitemValue>
                    <TickerBrokerStandardDateLineitemValue>
                      <TabName>Consensus Model2</TabName>
                      <StandardDate>2Q 2010</StandardDate>
                      <BRTab>Income Statement</BRTab>
                      <BRLineItem>NET REVENUES</BRLineItem>
                      <Action>Extracted</Action>
                      <StandardLineItem>Net Revenue</StandardLineItem>
                      <StandardValue>454.776</StandardValue>
                    </TickerBrokerStandardDateLineitemValue>
                    <TickerBrokerStandardDateLineitemValue>
                      <TabName>Consensus Model3</TabName>
                      <StandardDate>2Q 2010</StandardDate>
                      <BRTab>Income Statement</BRTab>
                      <BRLineItem>NET REVENUES</BRLineItem>
                      <Action>Extracted</Action>
                      <StandardLineItem>Net Revenue</StandardLineItem>
                      <StandardValue></StandardValue>
                    </TickerBrokerStandardDateLineitemValue>
                    <TickerBrokerStandardDateLineitemValue>
                      <TabName>Consensus Model4</TabName>
                      <StandardDate>2Q 2010</StandardDate>
                      <BRTab>Income Statement</BRTab>
                      <BRLineItem>NET REVENUES</BRLineItem>
                      <Action>Extracted</Action>
                      <StandardLineItem>Net Revenue</StandardLineItem>
                      <StandardValue/>
                    </TickerBrokerStandardDateLineitemValue>
                  </TickerBrokerStandardDateLineitemValues>
                </TickerBrokerStandardDateLineitem>";
    
                doc.Descendants("TickerBrokerStandardDateLineitemValue").Where(x => string.IsNullOrEmpty((string)x.Element("StandardValue"))).Remove();
                Console.WriteLine(doc.ToString());

    Wednesday, July 31, 2019 10:59 AM
  • can you please tell me how to do the same with xmldocument class instead of xdocument

    thanks

    Wednesday, July 31, 2019 11:01 AM
  • Hi Sudip_inn,

    You could create a new thread in c# forum.

    Thanks for understanding.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 1, 2019 5:10 AM
    Moderator