XML WriteAttributeString error

孤人 提交于 2019-12-10 16:17:50

问题


When I write this entry here:

<XmlRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nsSBAK" xsi:schemaLocation ="urn:nsSBAK SBAK.xsd"> 

with this code:

xmlWriter.WriteStartElement("XmlRoot");
xmlWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
xmlWriter.WriteAttributeString("xmlns", null, null, "urn:nsSBAK");
xmlWriter.WriteAttributeString("schemaLocation", null, "urn:nsSBAK SBAK.xsd");

I get debug error:

The prefix '' cannot be redefined from '' to 'urn:nsSBAK' within the same start element tag.

Can you help me ?


回答1:


You need to define the namespace of the element on the WriteStartElement itself. Also noticed you did not add the namespace to your schemaLocation. wich you dit in your desired result. Also added that for you in my example:

xmlWriter.WriteStartElement("XmlRoot", "urn:nsSBAK");
xmlWriter.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "urn:nsSBAK SBAK.xsd");


来源:https://stackoverflow.com/questions/6048695/xml-writeattributestring-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!