none
How can I get all the sqls under the Customer tag id RRS feed

  • Question

  • I want to get the all sqls under  one tag id. When I call the function GETSQL , the  _resultxml.tagid would be 'CUSTOMER1'. how can I get all the sqls under the tagid 'CUSTOMER1. How can I get all the sql under the customer tag id 'CUSTOMER1' FROM  xml

    public static xmlResult GetSQL(XmlNode element) { _resultxml.Tagid = element.Attributes["ID"].Value;

     List<string> sql_strings1 = ((IEnumerable<object>)xml1.XPathEvaluate("/*/CUSTOMER/SQL/comment()"))
                           .Cast<XComment>()
                           .Select(c => c.Value.Trim())
                           .ToList();

    // in xpathEvaluate argument i want to use Tagid value rather than element.name. }

    xml

    <?xml version="1.0" encoding="utf-8" ?>
    <STATEMENT>
    <CUSTOMER ID = "CUSTOMER1">
    <SQL ID="SQLS1">
     <!--
    	SELECT * FROM CUSTOMER WHERE CODE = '123'
    -->
     </SQL>
    <SQL ID="SQLS2">
    <!--
     SELECT * FROM CUSTOMER WHERE CODE = '567'
    -->
     </SQL>
    <Address>test@gmail.com</Address>
    <Address>test1@gmail.com</Address>
    <Address></Address>
    <MailSubject>Please find statement</MailSubject>
    <MailBody>Please find statement</MailBody>
    
    <FILENAME>file1</FILENAME>
    
    </CUSTOMER>
    <CUSTOMER ID = "CUSTOMER2">
    <SQL ID="SQLS">
      <!-- 
     SELECT * FROM CUSTOMER WHERE CODE = '1234' 
    -->
     </SQL>
    <Address>test@gmail.com</Address>
    <Address>test1@gmail.com</Address>
    <Address></Address>
    <MailSubject>Please find statement</MailSubject>
    <MailBody>Please find statement</MailBody>
    <FILENAME>file2</FILENAME>
    
    </CUSTOMER>
    </STATEMENT>


    polachan



    • Edited by polachan Thursday, July 4, 2019 2:25 PM
    Thursday, July 4, 2019 2:23 PM

Answers

  • If element is a <CUSTOMER> node, then try this:

    public static void GetSQL( XmlNode element )
    {
        string id = element.Attributes["ID"].Value;
    
        List<string> comments = 
            element
                .SelectNodes( "SQL/comment()" )
                .Cast<XmlComment>( )
                .Select( c => c.Value.Trim() )
                .ToList();
    
    }
    

    Then adjust it to return the required object.

    • Marked as answer by polachan Friday, July 5, 2019 6:11 AM
    Thursday, July 4, 2019 5:05 PM

All replies

  • If element is a <CUSTOMER> node, then try this:

    public static void GetSQL( XmlNode element )
    {
        string id = element.Attributes["ID"].Value;
    
        List<string> comments = 
            element
                .SelectNodes( "SQL/comment()" )
                .Cast<XmlComment>( )
                .Select( c => c.Value.Trim() )
                .ToList();
    
    }
    

    Then adjust it to return the required object.

    • Marked as answer by polachan Friday, July 5, 2019 6:11 AM
    Thursday, July 4, 2019 5:05 PM
  • Hi polachan, 

    Thank you for posting here.

    I have made a sample on my side based on your XML, you can use GetSQL() method to get the all sqls.

    Here’s the code:

            public static List<string> GetSQL(string path)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                List<string> ls = new List<string>();
                XmlNodeList nodes = doc.GetElementsByTagName("SQL");
                foreach (XmlNode node in nodes)
                {
                    foreach (XmlNode xn in node.ChildNodes)
                    {
                        if (xn.NodeType == XmlNodeType.Comment)
                        {
                            ls.Add(xn.InnerText.TrimStart());
                        }
                    }
                }
                return ls;
            }

    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.

    • Marked as answer by polachan Friday, July 5, 2019 6:11 AM
    • Unmarked as answer by polachan Friday, July 5, 2019 6:27 AM
    Friday, July 5, 2019 5:23 AM
    Moderator