2

For this xml (in a SQL 2005 XML column):

<doc> 
 <a>1</a> 
 <b ba="1" bb="2" bc="3" /> 
 <c bd="3"/> 
<doc> 

I'd like to be able to retrieve the names of the attributes (ba, bb, bc, bd) rather than the values inside using C# windows form application.

5
  • Haven't you just asked this one before? stackoverflow.com/questions/4237327/… Commented Nov 21, 2010 at 11:35
  • yes but that wasn't the exact answer which helps me... i hope i could make it clear now Commented Nov 21, 2010 at 11:36
  • Salman your Xml is not in correct format, </doc> should be at end, You can skip attr.Value in the code that I posted for you, if you fix your current xml issue you will get ba, bb,bc,bd also you should remove extra outputs. Commented Nov 21, 2010 at 12:07
  • If there is ambiguity in code comment it to explain it Commented Nov 21, 2010 at 12:12
  • the first answer in your other question stackoverflow.com/questions/4237327/… shows exactly how to extract the attribute names from your XML. Commented Nov 21, 2010 at 12:17

2 Answers 2

1

Try something like this - the first method will load the XML string from the database (adjust the connection string and query to your own database, server, table, column names), and the second method will parse the XML string loaded from the database into a list of attribute names based on the answer you got for your previous question:

 static void Main(string[] args)
    {
        string xmlContent = GrabStringFromDatabase(1);
        List<string> attributeNames = ParseForAttributeNames(xmlContent);

        Console.WriteLine("Your XML attributes are: {0}", string.Join(",", attributeNames.ToArray()));
    }

    private static string GrabStringFromDatabase(int ID)
    {
        string result = string.Empty;
        string connection = "server=(local);database=test;integrated security=SSPI";
        string query = "SELECT XmlContent FROM dbo.TestXml WHERE ID = @ID";

        using(SqlConnection _con = new SqlConnection(connection))
        using (SqlCommand _cmd = new SqlCommand(query, _con))
        {
            _cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            _con.Open();
            result = _cmd.ExecuteScalar().ToString();
            _con.Close();
        }

        return result;
    }

    private static List<string> ParseForAttributeNames(string xmlContent)
    {
        List<string> attributeNames = new List<string>();

        XDocument xmlDoc = XDocument.Parse(xmlContent);

        var nodeAttrs = xmlDoc.Descendants().Select(x => x.Attributes());

        foreach (var attrs in nodeAttrs)
        {
            foreach (var attr in attrs)
            {
                attributeNames.Add(attr.Name.LocalName);
            }
        }

        return attributeNames;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

There are two ways to do this in your case:

  • If you can create stored procedures on your SQL server:
    You can create a stored procedure that disassembles the XML into columns, and easily SELECT them and have the C# app handle it as if it were a table with columns and rows.

  • If you can't:
    You can disassemble the XML in C# using SqlXml from the System.Xml namespace.

Both ways are explained with examples very well here:
http://msdn.microsoft.com/en-us/library/ms345117(SQL.90).aspx#sql2k5xml_topic4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.