====一个最简单的Net操作XML的程序,居然通不过,请各位帮忙看一下!!=====

alj 2005-03-20 11:57:54
程序如下:

private void btnXmlTest_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc=new XmlDocument();
XmlNode ndRoot=xmlDoc.CreateNode(XmlNodeType.Element,"Book","");
SetNodeAttribute(ref ndRoot,"Name","sdfasfd asdf");
xmlDoc.DocumentElement.AppendChild(ndRoot);
tbInfo2.Text=xmlDoc.OuterXml;
}

private void SetNodeAttribute(ref System.Xml.XmlNode node,string attriName,string attriValue)
{
XmlDocument xDoc=new XmlDocument();
XmlAttribute tNode=xDoc.CreateAttribute(attriName);
tNode.Value=attriValue;
node.Attributes.Append(tNode);
}

我的目的就像想得到如下的XML文本:
<?xml version="1.0"?>
<Book Name="asdfasdf asd"/>

那里出错啦?
...全文
117 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
速马 2005-03-20
  • 打赏
  • 举报
回复
XML上下文和.NET的XML解析器实现有关
速马 2005-03-20
  • 打赏
  • 举报
回复
// XmlDocument xDoc=new XmlDocument();
// XmlAttribute tNode=xDoc.CreateAttribute(attriName);

只能在一个XML上下文操作
new XmlDocument()相当于一个新的XML上下文,和你要操作的node不同
alj 2005-03-20
  • 打赏
  • 举报
回复
AllenTing(It's time to 闭关修炼ing!!) :

谢谢你的回复:
为什么这样表示node.Attributes.Append(tNode);不行?
AllenTing 2005-03-20
  • 打赏
  • 举报
回复
看了下,这有问题:
private void SetNodeAttribute(ref System.Xml.XmlNode node,string attriName,string attriValue , XmlDocumnet xd)
{
//XmlDocument xDoc=new XmlDocument();//应该是这有问题,加一个xmldocument参数
XmlAttribute tNode=xd.CreateAttribute(attriName);
tNode.Value=attriValue;
node.Attributes.Append(tNode);
}
AllenTing 2005-03-20
  • 打赏
  • 举报
回复
private void SetNodeAttribute(ref System.Xml.XmlNode node,string attriName,string attriValue)
{
XmlDocument xDoc=new XmlDocument();
XmlAttribute tNode=xDoc.CreateAttribute(attriName);
tNode.Value=attriValue;
node.Attributes.Append(tNode);
}
------------------------------------------------------------------>
private void SetNodeAttribute(ref System.Xml.XmlNode node,string attriName,string attriValue)
{
XmlDocument xDoc=new XmlDocument();
XmlAttribute tNode=xDoc.CreateAttribute(attriName);
tNode.Value=attriValue;
// node.Attributes.Append(tNode);
(XmlElement)node.SetAttributeNode(tNode);

}
alj 2005-03-20
  • 打赏
  • 举报
回复
错误提示:
命名节点来自不同的文档上下文

什么意思?
wjcking 2005-03-20
  • 打赏
  • 举报
回复
XML上下文和.NET的XML解析器实现有关

110,534

社区成员

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

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

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