开发者

XML using in C#

I have connected my C# winforms app to SQL Server 2005 where is a column of XML data type.

The C# program is performing the ADD, DELETE, UPDATE and SELECT queries properly, now the next step is to get the xml which is stored in the database through query in a string which I did successfully and showed in MessageBox,

My problem is that I want the attributes of the xml and values show in the GridView while ignoring the other tags, please help me with running source code cause just refering to the functions doesn't work. thanks.... suppose I execute reader.read(); whats next?

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        // setData();
        qry = "select ID , Name from xmlTB";
        reader = db.select_data(qry);
        while (reader.Read())
        {
            MessageBox.Show((reader[0].ToString() +reader[1].ToString()));
        }
    }
    catch (Exception ex)
    { }
}

database connections are in di开发者_JAVA技巧fferent class but don't worry everything is working just need next step please


You need to select specific values only. Use LINQ-to-XML to extract the needed values from the XML you have.

You can use XDocument.Parse to transform the raw data to a query-able object.

For example:

XDocument document = XDocument.Parse(rawXmlString);

// Iterates through each element inside the XML document
foreach (XElement el in document.Root.Elements())
{
    // Iterates through each attribute in an element
    foreach (XAttribute attribute in el.Attributes())
    {
        // Action here
    }
}


Start by creating a DataTable then reading the data. If you find an XmlAttribute which does not already have a column in the DataTable, add one, and then create a DataRow and set it's values.

Finally bind the GridView to the DataSource.

DataTable dt = new DataTable();
string qry = "select ID , Name from xmlTB";
SqlDataReader reader = db.select_data(qry);
dt.Columns.Add["ID"];
while (reader.Read())
{
    string xmlString = reader[1].ToString();
    string id = reader[0].ToString();
    XDocument document = XDocument.Parse(xmlString);
    //ensure datatable contains a column for each attribute:
    foreach (XElement el in document.Root.Elements())
    {
        foreach (XAttribute attribute in el.Attributes())
        {  
            if (!dt.Columns.Contains(attribute.Name.LocalName))
            {
                dt.Columns.Add(new DataColumn(attribute.Name.LocalName, typeof(string)));
            }
        }
    }
    DataRow dr = dt.NewRow();
    //set each value in the datarow:
    foreach (XElement el in document.Root.Elements())
    {
        foreach (XAttribute attribute in el.Attributes())
        {
            dr[attribute.Name.LocalName] = attribute.Value;
        }
    }
    dr["ID"] = id;
    dt.Rows.Add(dr);
}

//Bind the gridview to the datasource:
this.dataGridView1.DataSource = dt;
//*The above assumes your GridView is named dataGridView1.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜