none
how to get the sqls in a string variable RRS feed

  • Question

  • My XML is as given below

    <?xml version="1.0" encoding="UTF-8"?>
    -<QUERIES>
    -<SQL>
    <!-- Select Code from Customer1 -->
    </SQL>
    -<SQL>
    <!-- Select Code from Customer2 -->
    </SQL>

    I want to read each  sqls  in a loop using XDocument

     XDocument xml1 = XDocument.Load(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) +"\\Sql.xml");
                var groupElements = from el in xml1.Descendants().Elements("SQL") select el;
                foreach (XElement xe in groupElements)
                {
                    string ls =  How can I get the SQL stored  in each  SQL node here 
                }

    I  have never used before Xdocument before.


    polachan

    Thursday, February 15, 2018 4:21 PM

Answers

  • You could also use a DataSet e.g.

    List<string> Queries()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(System.IO.Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory, "sql.xml"));
        return ds.Tables[0].AsEnumerable()
            .Select(row => row.Field<string>(0)).ToList();
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by polachan Friday, February 16, 2018 11:04 AM
    Friday, February 16, 2018 1:26 AM
    Moderator

All replies

  • Try this:

    using System.Xml.XPath;

    . . .

    List<string> sql_strings =

       ( (IEnumerable<object>)xml1.XPathEvaluate( "/*/SQL/comment()" ) )

          .Cast<XComment>()

          .Select( c => c.Value.Trim() )

          .ToList();

     

    The list will contain all of the selected comments.


    • Edited by Viorel_MVP Thursday, February 15, 2018 6:51 PM
    Thursday, February 15, 2018 6:50 PM
  • You could also use a DataSet e.g.

    List<string> Queries()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(System.IO.Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory, "sql.xml"));
        return ds.Tables[0].AsEnumerable()
            .Select(row => row.Field<string>(0)).ToList();
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by polachan Friday, February 16, 2018 11:04 AM
    Friday, February 16, 2018 1:26 AM
    Moderator
  • Hi polachan,

    Thank you for posting here.

    For your question, please try the code below.

      XDocument xdoc = XDocument.Load("sqls.xml");
    
                var groupElements = from el in xdoc.Descendants().Elements("SQL") select el;
    
                //groupElements is now a collection of XElements
    
                foreach (XElement xe in groupElements)
                {
                    //do something
                    Console.WriteLine(xe.Value);
                }
                Console.ReadKey();

    In your code, I do not find any nodes of sqls, I do some change with your xml file.

    <?xml version="1.0" encoding="UTF-8"?>
    <QUERIES>
    <SQL>
    <SQLS>hello</SQLS>
    <!-- Select Code from Customer1 -->
    </SQL>
    <SQL>
    <!-- Select Code from Customer2 -->
    </SQL>
    </QUERIES>

    If I use the code to get the value of SQLS in SQL element, I could get hello.

    Best Regards,

    Wendy


    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.

    Friday, February 16, 2018 5:40 AM
    Moderator