C#XML - 与XML作家多个命名空间声明 [英] C# XML - Multiple Namespace Declaration with XML Writer

查看:105
本文介绍了C#XML - 与XML作家多个命名空间声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建在C#中使用System.Xml.Xmlwriter多个命名空间的XML文档,我recieving上编译以下错误:




前缀''一样开始元素标记中'不能从被重新定义'到'http://www.acme.com/BOF。




我的代码全部是如下:

  XmlWriterSettings设置=新XmlWriterSettings {编码= Encoding.UTF8 ,缩进= TRUE}; 

XmlWriter的作家= XmlWriter.Create(C:\\ACME\\xml.xml,设置);

writer.WriteStartDocument();

writer.WriteStartElement(BOF);
writer.WriteAttributeString(的xmlns,NULL,NULL,http://www.acme.com/BOF); //这是我得到我的错误
writer.WriteAttributeString(的xmlns,XSI,NULL,http://www.w3.org/2001/XMLSchema-instance);
writer.WriteAttributeString(文件名,NULL,NULL,的test.xml);
writer.WriteAttributeString(DATE,NULL,NULL,2011-10-25);
writer.WriteAttributeString(出身,NULL,NULL,MYORIGIN);
writer.WriteAttributeString(参考,NULL,NULL,XX_88888);
writer.WriteEndElement();

writer.WriteStartElement(CustomerNo);
writer.WriteString(12345);
writer.WriteEndElement();

writer.WriteEndDocument();

writer.Flush();
writer.Close();



我在做什么错了?



谢谢



约翰


解决方案

  writer.WriteStartElement(BOF); //写元素名称BOF,没有前缀,命名空间,
writer.WriteAttributeString(的xmlns,NULL,NULL,http://www.acme.com/BOF); //为没有前缀为http://www.acme.com/BOF设置的命名空间。



第二行是没有意义的,因为你分配默认的(无前缀)命名空间东西比它是什么其他的,在同一个地方,因为它是这一点。



替换这两行与 writer.WriteStartElement(BOF, http://www.acme.com/BOF)


I am trying to create an XML document with multiple namespaces using System.Xml.Xmlwriter in C# and am recieving the following error on compile:

The prefix '' cannot be redefined from '' to 'http://www.acme.com/BOF' within the same start element tag.

The entirety of my code is below:

        XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };

        XmlWriter writer = XmlWriter.Create("C:\\ACME\\xml.xml", settings);

        writer.WriteStartDocument();

        writer.WriteStartElement("BOF");
        writer.WriteAttributeString("xmlns", null, null, "http://www.acme.com/BOF");  //This is where I get my error
        writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
        writer.WriteAttributeString("fileName", null, null, "test.xml");
        writer.WriteAttributeString("date", null, null, "2011-10-25");
        writer.WriteAttributeString("origin", null, null, "MYORIGIN");
        writer.WriteAttributeString("ref", null, null, "XX_88888");
        writer.WriteEndElement();

        writer.WriteStartElement("CustomerNo");
        writer.WriteString("12345");
        writer.WriteEndElement();

        writer.WriteEndDocument();

        writer.Flush();
        writer.Close();

What am I doing wrong?

Thanks

John

解决方案

writer.WriteStartElement("BOF"); // write element name BOF, no prefix, namespace ""
writer.WriteAttributeString("xmlns", null, null, "http://www.acme.com/BOF");  //Set namespace for no prefix to "http://www.acme.com/BOF".

The second line makes no sense, because you're assigning the default (no-prefix) namespace to something other than what it is, in the same place as it is that.

Replace those two lines with writer.WriteStartElement("BOF", "http://www.acme.com/BOF")

这篇关于C#XML - 与XML作家多个命名空间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆