none
how can I get the selectednode attribute into the list<string> variable. Please help RRS feed

  • Question

  • Sorry for disturbing you all. This might be the last question from my side to read the xml file

    I want to get the SQL ID name into the list string variable. The list string should be stored with SQLS1, SQLS2

    My code

    foreach (var child in subjectList)
                        {
                            if (child is XmlNode)
                            {
                                var element = (XmlNode)child;
                                if (element.HasChildNodes)
                                {
                                    var attributes = element.Attributes;
                                    xmlData.Tagid = element.Attributes["ID"].Value;
                                    //xmlData = GetSQL(element);
                                    foreach (var item in attributes)
                                    {
                                        string id = element.Attributes["ID"].Value;
                                        xmlData.ListSql =
                                                element
                                                .SelectNodes("SQL/comment()")
                                                .Cast<XmlComment>()
                                                .Select(c => c.Value.Trim())
                                                .ToList();
    
                                        xmlData.EmailAddressList =
                                                element
                                                .SelectNodes("Address")
                                                .Cast<XmlNode>()
                                                .Select(c => c.InnerText.Trim())
                                                .ToList();
                                        xmlData.mailSubject =
                                                 element
                                                .SelectNodes("MailSubject")
                                                .Cast<XmlNode>()
                                                .Select(c => c.InnerText).FirstOrDefault();
                                        xmlData.mailBody =
                                                 element
                                                .SelectNodes("MailBody")
                                                .Cast<XmlNode>()
                                                .Select(c => c.InnerText).FirstOrDefault();
                                        xmlData.fileName =
                                                 element
                                                .SelectNodes("FileName")
                                                .Cast<XmlNode>()
                                                .Select(c => c.InnerText).FirstOrDefault();
                                        List<string> sheetID =
                                               element
                                               .SelectNodes("SQL")
                                               .Cast<XmlNode>()
                                               .Select(c => c.Attributes("ID")) Here not working ?????
                                               .ToList();
    
    
                                        
    Please help
    
                                    }
                                    _filename = CreateExcelFile(xmlData, _excelfolder,database);
    
                                }
                            }
                        }

    XML

    <?xml version="1.0" encoding="utf-8" ?>
    <STATEMENT>
    <CUSTOMER ID = "CUSTOMER1">
    <SQL ID="SQLS1">
         <!-- Select Rate From Product -->
     </SQL>
    <SQL ID="SQLS2">
         <!-- select Qty from Product -->
     </SQL>
    <Address>test1@gmail.com</Address>
    <Address>test2@gmail.com</Address>
    <Address></Address>
    <MailSubject>Statement</MailSubject>
    <MailBody>Please find statement</MailBody>
    <FILENAME>Statement2</FILENAME>
    </CUSTOMER>
    <CUSTOMER ID = "CUSTOMER2">
    <SQL ID="SQLS">
    <!-- Select Code from Customer -->
     </SQL>
    <SQL ID="SQLS3">
    <!-- select name from supplier -->
     </SQL>
    <SQL ID="SQLS4">
    <!-- select qty from purchase-->
     </SQL>
    
    <EmailAddress>test3@gmail.com</Address>
    <EmailAddress>test4@gmail.com</Address>
    <MailSubject>statement</MailSubject>
    <MailBody>Please find statement</MailBody>
    <FILENAME>Statement2</FILENAME>
    </CUSTOMER>
    </STATEMENT>


    polachan

    Friday, July 5, 2019 1:31 PM

All replies

  • Try this:

    List<string> sheetID =
                    element
                        .SelectNodes( "SQL" )
                        .Cast<XmlNode>( )
                        .Select( n => n.Attributes[ "ID" ].Value )
                        .ToList( );

    or this:

    List<string> sheetID =
                    element
                        .SelectNodes( "SQL/@ID" )
                        .Cast<XmlAttribute>( )
                        .Select( a => a.Value )
                        .ToList( );


    • Edited by Viorel_MVP Friday, July 5, 2019 7:23 PM
    Friday, July 5, 2019 7:22 PM
  • Hi  polachan, 

    Thank you for posting here.

    According to your question, you can also try the following code to get the SQL ID name:

                XDocument xd = XDocument.Load(path3);
                List<string> items = (from item in xd.Descendants("SQL")
                             select item.Attribute("ID").Value).ToList();

    Result of my test:

    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.

    Monday, July 8, 2019 7:28 AM
    Moderator