给xml的一个节点添加属性的问题!

0422 2004-08-26 05:54:25
System.Xml.XmlDocument docWrite=new System.Xml.XmlDocument();
docWrite.Load(sourceFile);
XmlNodeList nodeList=docWrite.SelectNodes("/Application/forms/action/form/items/action/Item/Specific[@caption='"+caption+"']");
foreach (System.Xml.XmlNode _Node in nodeList)
{

下面通不过。请高手指教,注意是添加属性。
System.Xml.XmlAttribute newDD_ID=new XmlAttribute();
newDD_ID.InnerText ="DD-ID";
newDD_ID.Value ="TestDD_ID";



docWrite.Save(sourceFile);
}
...全文
349 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
0422 2004-08-28
  • 打赏
  • 举报
回复
xpaht类型没发转换成xmelement类型的,请问应该怎么实现!
0422 2004-08-28
  • 打赏
  • 举报
回复
XPathNavigator navItem = iterator.Current.Clone(); //defines another Navigator to search the nodes inside

while (navItem.MoveToNextAttribute())
{
if (navItem.Name=="title") //scan the property of the caption if the caption exist then do below
{
bool DD_IDExist=false; //default it doesn't exist
title=navItem.Value ; //get teh node 's caption
XPathNavigator dd_idItem = iterator.Current.Clone(); //defines another Navigator to search the nodes inside
// TODO: below is not the best sulution,
// dd_idItem.MoveToFirstAttribute();
// while (dd_idItem.MoveToNextAttribute())
// {
// if (dd_idItem.Name=="DD-ID")
// {
// DD_IDExist=true;
// break;
// }
// }
BearRui 2004-08-27
  • 打赏
  • 举报
回复
XmlElement xmlele;

if(xmlele.HasAttributes)
{
foreach(XmlAttribute xmlatt in xmlele.Attributes)
{
Console.WriteLine(xmlatt.Value);
}
}
0422 2004-08-27
  • 打赏
  • 举报
回复
谢谢,那我怎么遍历它下面的所有属性然后显示出来
BearRui 2004-08-26
  • 打赏
  • 举报
回复
foreach (System.Xml.XmlNode _Node in nodeList)
{
XmlElement Xmlele=(XmlElement) _Node;
Xmlele.SetAttribute("newDD_ID","TestDD_ID");
}

docWrite.Save(sourceFile);

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧