none
get xml Element childNode and attribute values through c# RRS feed

  • Question

  • I am having a xml schema with nested xml elements and following is the small piece of that

    <aa>
    <id extension="xx" root="56" />
    <name>demo</name>
    <telecom use="EC" value="tel:02073333333" />
    </aa>
    
    <bb>
    
      <value value="201505100000" />
    </bb>
    <cc>
      <value value="201805100000" />
    </cc>

    from this i have to get the value of "name tag" under "aa tag", last attribute (tel:) of telecom tag, and attribute value of the "value tag" (which is found under bb tag and cc tag)

     i tried the following code, but it's not getting exactly what i am expecting.
    
    
    xDoc.Descendants().Where(x => x.Name.LocalName.Equals("aa)
                            || x.Name.LocalName.Equals("telecom") && (x.FirstAttribute.Equals("EC")
                            || x.Name.LocalName.Equals("bb)
                            || x.Name.LocalName.Equals("cc))
    
    
    please provide me the solution regarding this.
    Friday, January 11, 2019 4:30 AM

All replies

  • Add ‘using System.Xml.XPath’ and try this:

    string name = xDoc.XPathSelectElement( "//aa/name" )?.Value;

    string tel = xDoc.XPathSelectElement( "//aa/telecom" )?.Attribute( "value" )?.Value;

    string bb = xDoc.XPathSelectElement( "//bb/value" )?.Attribute( "value" )?.Value;

    string cc = xDoc.XPathSelectElement( "//cc/value" )?.Attribute( "value" )?.Value;

     

    Sample file:

    <?xml version="1.0" encoding="utf-8" ?>

    <data>

      <aa>

        <id extension="xx" root="56" />

        <name>demo</name>

        <telecom use="EC" value="tel:02073333333" />

      </aa>

      <bb>

        <value value="201505100000" />

      </bb>

      <cc>

        <value value="201805100000" />

      </cc>

    </data>

     

     

    If there are more such elements in the file, then give more details.


    • Edited by Viorel_MVP Friday, January 11, 2019 7:12 AM
    Friday, January 11, 2019 7:11 AM
  • thanks , i will try this
    Friday, January 11, 2019 4:52 PM
  • Hi Tried this , but i am returning null value, following is my actual schema will look like (edited due to confidential)<rootabc xmlns="aaaaa"> <ControlAct><record><communicationFunctionSnd><organisation> <id extension="cfff" root="2.8999" /> <name>demo</name><telecom use="EC" value="tel:123445" /></organisation></communicationFunctionSnd> <demo2<info15><npfitlc:contentId/><Information><component><startdate><value value="201505100000" /></startdate></component <component1> <enddate><value value="201805100000" /> </enddate> </component1></<Information></info15> </control>
    Monday, January 14, 2019 3:18 AM
  • Hi siddmuthu,

    Thank you for posting here.

    According to your description, you want to get some value of the xml.

    You could try this code below.

    static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(@"D:\ test11.xml");
                XmlNode node = doc.DocumentElement.SelectSingleNode("/data/aa/name");
                Console.WriteLine(node.InnerText);
                XmlNode node1 = doc.DocumentElement.SelectSingleNode("/data/aa/telecom");
                Console.WriteLine(node1.Attributes["value"].Value);
                XmlNode node2 = doc.DocumentElement.SelectSingleNode("/data/bb/value");
                Console.WriteLine(node2.Attributes["value"].Value);
                XmlNode node3 = doc.DocumentElement.SelectSingleNode("/data/cc/value");
                Console.WriteLine(node3.Attributes["value"].Value);
                Console.ReadKey();
            }
    

    Result:

    Hope my suggestion could be helpful.

    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.

    Monday, January 14, 2019 8:35 AM
    Moderator