读书人

下面这段XML如何操作呀

发布时间: 2011-12-23 23:32:01 作者: rapoo

下面这段XML怎么操作呀?
<Catalog>
<Title> 栏目管理 </Title>
<Items>
<Item> 栏目管理0 <Item>
<Item> 栏目管理1 <Item>
<Item> 栏目管理2 <Item>
</Items>
</Catalog>
1、怎么样让dropdownlist读出 <Title> </Title> 里边的内容,然后ListBox自动读取Item里的内容;
2、怎么样把TextBox里的值加到相应的 <Title> </Title> 下边的Item里
3、删除相应的!




[解决办法]

[解决办法]
呵呵,人,那我主解吧!
XMLFile.xml文件容:


<?xml version= "1.0 " encoding= "utf-8 " ?>
<Catalog>
<Title> 栏目管理
<Items>
<Item>
栏目管理0
</Item>
<Item>
栏目管理1
</Item>
<Item>
栏目管理2
</Item>
</Items>
</Title>
<Title> 源管理
<Items>
<Item>
源管理0
</Item>
<Item>
源管理1
</Item>
<Item>
源管理2
</Item>
</Items>
</Title>
</Catalog>


//回1:
XmlDocument xdc = new XmlDocument();
xdc.Load(Server.MapPath( "XMLFile.xml "));

XmlNodeList xnl = xdc.SelectSingleNode( "Catalog ").ChildNodes;
foreach (XmlNode node in xnl)
{
if (node.Name == "Title ")
{
DropDownList1.Items.Add(new ListItem(node.FirstChild.Value.Trim()));
}
}
xdc = null;
[解决办法]
中DropDownList1中的Title容示在ListBox1,
有前提件是把DropDownList1的autopostbackTrue;

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDocument xdc = new XmlDocument();
xdc.Load(Server.MapPath( "XMLFile.xml "));
ListBox1.Items.Clear();
XmlNodeList xnl = xdc.SelectSingleNode( "Catalog ").ChildNodes;
foreach (XmlNode node in xnl)
{
if (node.FirstChild.Value.Trim() == DropDownList1.SelectedValue)
{
XmlNodeList xnl2 = node.LastChild.ChildNodes;
foreach (XmlNode node2 in xnl2)
{

ListBox1.Items.Add(new ListItem(node2.InnerText.Trim()));
}
}
}
xdc = null;
}
[解决办法]
2、怎么样把TextBox里的值加到相应的 <Title> </Title> 下边的Item里
3、删除相应的!
---
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Load(Server.MapPath( "XMLFile.xml ")));
XmlNodeList nodeList=xmlDoc.SelectSingleNode( "Catalog ").ChildNodes;
foreach(XmlNode xn in nodeList)
{
if (xn.Name== "Title ")
{
foreach(XmlNode xmlnode in NodeList)
{
if(xmlnode.InnerText.ToString()==strText)
{
xn.LastChild.RemoveChild(xmlnode);//delete node


}
}
XmlNodeList NodeList=xn.LastChild.ChildNodes;
string strText=text.Text.ToString();
XmlElement xe1=xmlDoc.CreateElement( "Item ");
xe1.InnerText=strText;xn.LastChild.AppendChild(xe1);//add node
}
}

读书人网 >asp.net

热点推荐