English 中文(简体)
xml 中的命名空间问题
原标题:namespace issue in xml
XmlElement xmlElementSAPD = xmlDocument.CreateElement("SAPD");
root.AppendChild(xmlElementSAPD);

xmlElementSAPD.AppendChild(XmlFunctions.GetXMLElement(xmlDocument, "smu", dr.GetString("sma").Trim()));

上面的 c# 代码在下面创建 XML 。

<SAPD>
<smu>123</smu>
</SAPD>

我怎样才能改变上面的代码 才能得到

<ns0:SAPD>
<ns0:smu>123</ns0:smu>
</ns0:SAPD>

有人吗? 我怎么能在 xml 节点中添加 ns0?

问题回答

您必须在 xml 文档中添加命名空间 。

只是想给你个想法 看看下面

XmlDocument doc = new XmlDocument();  
XmlSchema schema = new XmlSchema();
schema.Namespaces.Add("ns0", "http://www.sample.com/file");
doc.Schemas.Add(schema);

您的前缀 ns0 需要用名称空间绘制, 否则 xml 无效 。

asumning ns0 绘制到http://tempuri.org ,以下代码可用:

        XmlDocument doc = new XmlDocument();
        XmlElement sapd= doc.CreateElement("ns0","SAPD","http://tempuri.org");
        XmlElement smu = doc.CreateElement("ns0", "smu", "http://tempuri.org");
        smu.InnerText = "123";
        sapd.AppendChild(smu);
        doc.AppendChild(sapd);
        doc.InnerXml.ToString();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签