none
How to find root element of element when xml load using XDocument RRS feed

  • Question

  • please give me sample code which take a element name and tell me its root element name. i am using XDocument class to load xml. 

    this is my xml

    <?xml version="1.0" encoding="utf-8"?>
    <TickerBrokerDateFormatMap BrokerTab_Id="0" Broker_Id="0" Ticker_Id="AMZN">
      <StandardDate>1Q 2010A</StandardDate>
      <ColumnCoordinate>X</ColumnCoordinate>
      <TickerBrokerDateFormatMaps_Id>0</TickerBrokerDateFormatMaps_Id>
      <BrokerDate StandardDate="1Q 2010A" Broker_Id="0" BrokerTab_Id="0">
        <year>1Q10</year>
        <Quater>1Q10</Quater>
      </BrokerDate>
    </TickerBrokerDateFormatMap>

    XDocument XDoc = XDocument.Load(xmlFilePath);
    var items = (from item in XDoc.Descendants("TickerBrokerDateFormatMap")
    	 select item).ToList(); 
    	 
    if (items.Elements("year")!=null)
    {
    	var xx = items.Elements("year").FirstOrDefault().Parent;
    }	 
    i try to find parent node name of year node but getting error. parent node of year is BrokerDate. so why i am getting error. what to change in my code ?

    thanks

    • Edited by Sudip_inn Monday, November 18, 2019 8:15 PM
    Monday, November 18, 2019 7:59 PM

Answers

  • Hi Sudip_inn, 

    Thank you for posting here.

    ‘items.Elements("year")’ method returns a filtered collection of the child elements of every element and document in the source collection, but ‘year’ element is not the child element of ‘TickerBrokerDateFormatMap’, so you get the error.

    Replace ‘items.Elements("year")’ with ‘items.Descendants("year")’.

                string xmlFilePath = @"xml path ";
                XDocument XDoc = XDocument.Load(xmlFilePath);
                var items = (from item in XDoc.Descendants("TickerBrokerDateFormatMap")
                             select item).ToList();
    
                if (items.Descendants("year") != null)
                {
                    var xx = items.Descendants("year").FirstOrDefault().Parent;
                    Console.WriteLine(xx.Name);
                }
                Console.ReadLine();
    

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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 Friday, November 22, 2019 12:26 PM
    Tuesday, November 19, 2019 6:36 AM
    Moderator
  • Hi Sudip_inn, 

    Thanks for your feedback.

    I create a xml file and make a test to get the element parent name.

    My xml file.

    If the child element is unique to the parent element, you can refer to the following code.

                XDocument XDoc = XDocument.Load(xmlFilePath);
                var element = XDoc.Descendants("office").Select(i => i.Parent).FirstOrDefault();
    

    Result:

    Otherwise, you need to filter based on the characteristics of the element.

                XDocument XDoc = XDocument.Load(xmlFilePath);
                var element = XDoc.Descendants("id").Where(i=>i.Value.Contains("S")).Select(i => i.Parent).FirstOrDefault();
    

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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.

    Wednesday, November 20, 2019 8:43 AM
    Moderator

All replies

  • Hi Sudip_inn, 

    Thank you for posting here.

    ‘items.Elements("year")’ method returns a filtered collection of the child elements of every element and document in the source collection, but ‘year’ element is not the child element of ‘TickerBrokerDateFormatMap’, so you get the error.

    Replace ‘items.Elements("year")’ with ‘items.Descendants("year")’.

                string xmlFilePath = @"xml path ";
                XDocument XDoc = XDocument.Load(xmlFilePath);
                var items = (from item in XDoc.Descendants("TickerBrokerDateFormatMap")
                             select item).ToList();
    
                if (items.Descendants("year") != null)
                {
                    var xx = items.Descendants("year").FirstOrDefault().Parent;
                    Console.WriteLine(xx.Name);
                }
                Console.ReadLine();
    

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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 Friday, November 22, 2019 12:26 PM
    Tuesday, November 19, 2019 6:36 AM
    Moderator
  • tell me how to get parent element name of any element ?
    Tuesday, November 19, 2019 3:43 PM
  • Hi Sudip_inn, 

    Thanks for your feedback.

    I create a xml file and make a test to get the element parent name.

    My xml file.

    If the child element is unique to the parent element, you can refer to the following code.

                XDocument XDoc = XDocument.Load(xmlFilePath);
                var element = XDoc.Descendants("office").Select(i => i.Parent).FirstOrDefault();
    

    Result:

    Otherwise, you need to filter based on the characteristics of the element.

                XDocument XDoc = XDocument.Load(xmlFilePath);
                var element = XDoc.Descendants("id").Where(i=>i.Value.Contains("S")).Select(i => i.Parent).FirstOrDefault();
    

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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.

    Wednesday, November 20, 2019 8:43 AM
    Moderator