在xml文档对象中添加节点出错

csxbbb 2003-11-20 04:58:54
System.Xml.XmlElement disAgreeNodeId=oXmlDocument.CreateElement("DisAgreeNodeId");
disAgreeNodeId.Value=node.DisAgreeNodeId.ToString();//此句出错:不能在节点类型上设置值: Element。
请指点
...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
csxbbb 2003-11-20
  • 打赏
  • 举报
回复
解决了
谢谢!!!
沈逸 2003-11-20
  • 打赏
  • 举报
回复
System.Xml.XmlElement disAgreeNodeId=oXmlDocument.CreateElement("DisAgreeNodeId");
disAgreeNodeId.Value=node.DisAgreeNodeId.ToString();//此句出错:不能在节点类型上设置值: Element。

---------------------------------------------
要设置为
disAgreeNodeId.InnerText="............"
具体请看
msdn关于 XmlElement类
orcale 2003-11-20
  • 打赏
  • 举报
回复
disAgreeNodeId.Value應該是只讀
using System;
using System.IO;
using System.Xml;

public class Sample
{
public static void Main()
{
Sample test = new Sample();
}

public Sample()
{
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<?xml version=\"1.0\"?>" +
"<!-- Sample XML document -->" +
"<bookstore xmlns:bk=\"urn:samples\">" +
" <book genre=\"novel\" publicationdate=\"1997\" " +
" bk:ISBN=\"1-861001-57-5\">" +
" <title>Pride And Prejudice</title>" +
" <author>" +
" <first-name>Jane</first-name>" +
" <last-name>Austen</last-name>" +
" </author>" +
" <price>24.95</price>" +
" </book>" +
" <book genre=\"novel\" publicationdate=\"1992\" " +
" bk:ISBN=\"1-861002-30-1\">" +
" <title>The Handmaid's Tale</title>" +
" <author>" +
" <first-name>Margaret</first-name>" +
" <last-name>Atwood</last-name>" +
" </author>" +
" <price>29.95</price>" +
" </book>" +
"</bookstore>");

XmlNode currNode = doc.DocumentElement;

//create and add a new element
string prefix=currNode.GetPrefixOfNamespace("urn:samples");
XmlElement newElem=doc.CreateElement(prefix, "style", "urn:samples");
newElem.InnerText="hardcover";

currNode.FirstChild.AppendChild(newElem);

Console.WriteLine("Display the modified XML...");
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
doc.WriteTo( writer );
writer.Flush();
writer.Close();

}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}

}
}
csxbbb 2003-11-20
  • 打赏
  • 举报
回复
node.DisAgreeNodeId.ToString()
只是一个字符串值
CinnXu 2003-11-20
  • 打赏
  • 举报
回复
node.DisAgreeNodeId.ToString()//这个是什么啊

把代码贴尽可能多一点,能表达清楚一点

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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