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;
}
</doc>should be at end, You can skipattr.Valuein the code that I posted for you, if you fix your current xml issue you will getba, bb,bc,bdalso you should remove extra outputs.