【发布时间】:2011-02-15 08:00:09
【问题描述】:
如何获取以下代码以添加带有“xmlns=''”的元素?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string strXML =
"<myroot>" +
" <group3 xmlns='myGroup3SerializerStyle'>" +
" <firstname xmlns=''>Neal3</firstname>" +
" </group3>" +
"</myroot>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strXML);
XmlElement elem = xmlDoc.CreateElement(null, "lastname", null);
elem.InnerText = "New-Value";
string strXPath = "/myroot/*[local-name()='group3' and namespace-uri()='myGroup3SerializerStyle']/firstname";
XmlNode insertPoint = xmlDoc.SelectSingleNode(strXPath);
insertPoint.AppendChild(elem);
string resultOuter = xmlDoc.OuterXml;
Console.WriteLine("\n resultOuter=" + resultOuter);
Console.ReadLine();
}
}
}
我目前的输出:
resultOuter=<myroot><group3 xmlns="myGroup3SerializerStyle"><firstname xmlns=""
>Neal3<lastname>New-Value</lastname></firstname></group3></myroot>
想要的输出:
resultOuter=<myroot><group3 xmlns="myGroup3SerializerStyle"><firstname xmlns=""
>Neal3<lastname xmlns="">New-Value</lastname></firstname></group3></myroot>
有关背景,请参阅相关帖子: http://www.stylusstudio.com/ssdn/default.asp?fid=23(今天)
.NET XmlSerializer to Element FormDefault=Unqualified XML?(3 月 9 日,以为我修好了,今天又咬我了!)
【问题讨论】:
标签: c# xml xmldocument