<?xml version="1.0" encoding="utf-8" ?>
- <root>
<version>1.0</version>
<modified>2011-6-30 9:48:18</modified>
<seller_id>官网</seller_id>
<cat_url>http://baidu.com</cat_url>
<dir>http://baidu.com</dir>
- <item_ids>
<outer_id action="upload">ID1000018414</outer_id>
</item_ids>
</root>我想在这基础上插入一条<outer_id action="upload">ID1000018414</outer_id>
会的分享下,谢谢
- <root>
<version>1.0</version>
<modified>2011-6-30 9:48:18</modified>
<seller_id>官网</seller_id>
<cat_url>http://baidu.com</cat_url>
<dir>http://baidu.com</dir>
- <item_ids>
<outer_id action="upload">ID1000018414</outer_id>
</item_ids>
</root>我想在这基础上插入一条<outer_id action="upload">ID1000018414</outer_id>
会的分享下,谢谢
解决方案 »
- 介绍一个学习asp.net管理系统的程序
- 散分两百,讨论一下高发送量邮件服务方案的设计
- 如何使用JavaScript把服务器端的XML文件读取到页面的文件 并将所有的数据能够放到table的dataSrc中阿?
- 关于sharpnuke的转换问题?
- 中文变英文
- 我用window.showModalDialog打开一个子窗口,为什么在提交的时候,会出现一个要求刷新的页面...
- ASP.NET如何实现ASP中FORM的功能啊?
- Web中间层和数据库连接问题(效率问题),请高手帮忙!
- 编译时没有问题,浏览时报的错误CS1595: 已在多处定义IntelligentOffice.Desktop._Default
- 怎么返回一个控件集?
- 求解,dropdownList
- 后台怎么获取HTML控件里的值
public static void Insert(string path, string node, string element, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(attribute, value);
}
}
else
{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xn.AppendChild(xe);
}
doc.Save(path);
}
catch { }
}
XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("~/test.xml"));
XmlNode item_ids = xml.GetElementsByTagName("item_ids")[0];
XmlElement newElement = xml.CreateElement("outer_id");
newElement.SetAttribute("action", "upload");
newElement.InnerText = "ID1000018414";
item_ids.AppendChild(newElement);
xml.Save(Server.MapPath("~/test.xml"));
xmlDoc.Load(Server.MapPath("~/etao/IncrementIndex.xml")); XmlNode objRootNode = xmlDoc.SelectSingleNode("root");
XmlElement objChildNode = xmlDoc.CreateElement("item_ids");
objRootNode.AppendChild(objChildNode);
XmlElement objElement = xmlDoc.CreateElement("outer_id");
objElement.InnerText = "lucky";
objChildNode.AppendChild(objElement);
xmlDoc.Save(Server.MapPath("~/etao/IncrementIndex.xml"));结果:
<?xml version="1.0" encoding="utf-8" ?>
- <root>
<version>1.0</version>
<modified>2011-6-30 9:48:18</modified>
<seller_id>官网</seller_id>
<cat_url>http:///etao/SellerCats.xml</cat_url>
<dir>http:///etao/item/</dir>
- <item_ids>
<outer_id action="upload">ID1000018414</outer_id>
</item_ids>
- <item_ids>
<outer_id>lucky</outer_id>
</item_ids>
</root>
如果XmlNode objRootNode = xmlDoc.SelectSingleNode("root");
换成XmlNode objRootNode = xmlDoc.SelectSingleNode("item_ids");就报错