日期:2014-05-19  浏览次数:20927 次

一个关于xml中AppendChild的问题
XmlElement   elem   =   xmldoc.CreateElement( "Element ");
//在elem中加入几个XmlElement
//...
//
xmldoc.DocumentElement.AppendChild(elem);

<?xml   version= "1.0 "   encoding= "UTF-8 "   standalone= "yes "   ?>  
  <Settings>
  <Element   xmlns= " ">
<> ....
  </Element>
  </Settings>
在将elem加入xmldoc后,多了xmlns= " ",
为什么会多了这一句,请问怎么把这一句给去掉,谢谢!

------解决方案--------------------
你那么创建xml我没用过,试试:
XmlDocument doc = new XmlDocument();
doc.LoadXml( " < " Settings "/> ");
XmlNode elem = xmldoc.DocumentElement(XmlNodeType.Element, "Element ", " ");
string RepXml = doc.OuterXml.Substring(0)

------解决方案--------------------
没有问题:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml( " <?xml version=\ "1.0\ " encoding=\ "UTF-8\ " standalone=\ "yes\ " ?> <Settings> </Settings> ");
XmlElement elem = xmldoc.CreateElement( "Element ");
//在elem中加入几个XmlElement
//...
//
xmldoc.DocumentElement.AppendChild(elem);
xmldoc.Save( "c:\\11.xml ");

--------------
输出结果
----------------
<?xml version= "1.0 " encoding= "UTF-8 " standalone= "yes " ?>
<Settings>
<Element />
</Settings>