如何将Xml数据显示到数据网格视图中 [英] How to display Xml data into datagrid view

查看:86
本文介绍了如何将Xml数据显示到数据网格视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好..



我是新的.net ...所以请帮助我





private void button3_Click(object sender,EventArgs e)

{

XmlTextReader reader = new XmlTextReader(filepath.xml);

reader.WhitespaceHandling = WhitespaceHandling.None;

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(reader);

DataSet ds = new DataSet();

ds.ReadXml(reader);

reader.Close();

XmlNode xnod = xmlDoc.DocumentElement;

AddWithChildren(xnod,1);

}





private void AddWithChildren(XmlNode xnod,Int32 intLevel)

{



XmlNode xnodWorking;

String strIndent = new string('''',2 * intLevel);



string strValue =(string)xnod.Value;

if(strValue!= null)

{

strValue = :+ strValue;

MessageBox.Show(strValue.ToString());

}





if(xnod.NodeType == XmlNodeType.Element)

{

XmlNamedNodeMap mapAttributes = xnod.Attributes;

lbNodes.Items.Add(strIndent ++ xnodAttribute.Name +

:+ xnodAttribute.Value);

//我在列表中添加数据



}



如果(xnod.HasChildNodes)

{

xnodWorking = xnod.FirstChild;

while(xnodWorking!= null)

{

AddWithChildren(xnodWorking,intLevel + 1);

xnodWorking = xnodWorking.NextSibling;

}

}

}

}







但是如果我想向dataGridview添加数据..如何我可以将数据添加到datagridview而不是list ..我想只显示xml的选定数据... lbNode是我的列表

解决方案

http://msdn.micr osoft.com/en-us/library/system.data.datatable.readxml.aspx [ ^ ]



DataTable.ReadXml就是你想要的。查看上面的链接以获取使用提示



一旦你有一个包含xml细节的数据表,那么就可以将数据库指定为网格视图数据源



http://msdn.microsoft.com/ en-us / library / fkx0cy6d.aspx [ ^ ]

hello..

Im new in .net ...so plz help me


private void button3_Click(object sender, EventArgs e)
{
XmlTextReader reader = new XmlTextReader("filepath.xml");
reader.WhitespaceHandling = WhitespaceHandling.None;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
DataSet ds = new DataSet();
ds.ReadXml(reader);
reader.Close();
XmlNode xnod = xmlDoc.DocumentElement;
AddWithChildren(xnod, 1);
}


private void AddWithChildren(XmlNode xnod, Int32 intLevel)
{

XmlNode xnodWorking;
String strIndent = new string('' '', 2 * intLevel);

string strValue = (string)xnod.Value;
if (strValue != null)
{
strValue = " : " + strValue;
MessageBox.Show(strValue.ToString());
}


if (xnod.NodeType == XmlNodeType.Element)
{
XmlNamedNodeMap mapAttributes = xnod.Attributes;
lbNodes.Items.Add(strIndent + " " + xnodAttribute.Name +
" : " + xnodAttribute.Value);
// im adding data into the list

}

if (xnod.HasChildNodes)
{
xnodWorking = xnod.FirstChild;
while (xnodWorking != null)
{
AddWithChildren(xnodWorking, intLevel + 1);
xnodWorking = xnodWorking.NextSibling;
}
}
}
}



But if i want to add data to dataGridview ..How i can add data to datagridview instead of list.. I want to display only selected data of xml...lbNode is my list

解决方案

http://msdn.microsoft.com/en-us/library/system.data.datatable.readxml.aspx[^]

DataTable.ReadXml is what you want. check out the link above for usage tips

Once you have a datatable containing the xml details then it''s a cinch to specify the database as the grid view datasource

http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx[^]


这篇关于如何将Xml数据显示到数据网格视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆