加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – XmlDocument – 前缀”无法从”重新定义为’X’

发布时间:2020-12-15 23:30:32 所属栏目:百科 来源:网络整理
导读:我这样做: var xml = new XmlDocument(); xml.AppendChild(xml.CreateXmlDeclaration("1.0",Encoding.UTF8.BodyName,"yes")); var el = (XmlElement)xml.AppendChild(xml.CreateElement("Document")); el.SetAttribute("xmlns","urn:iso:std:iso:20022:tech
我这样做:

var xml = new XmlDocument();
 xml.AppendChild(xml.CreateXmlDeclaration("1.0",Encoding.UTF8.BodyName,"yes"));
 var el = (XmlElement)xml.AppendChild(xml.CreateElement("Document"));           
 el.SetAttribute("xmlns","urn:iso:std:iso:20022:tech:xsd:" + SepaSchemaUtils.SepaSchemaToString(schema));

然后为了获得缩进的XML,我做了:

StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings
{
    Indent = true,IndentChars = "  ",NewLineChars = "rn",NewLineHandling = NewLineHandling.Replace
};
using (XmlWriter writer = XmlWriter.Create(sb,settings))
{
    doc.Save(writer);
}

我执行doc.Save(writer)时遇到异常.

System.Xml.XmlException: The prefix ” cannot be redefined from ” to
‘urn:iso:std:iso:20022:tech:xsd:pain.001.001.03’ within the same start
element tag.

我已经尝试过我能找到的一切.谢谢.

解决方法

您尝试创建不在命名空间中的Document元素,但同时设置默认命名空间.我怀疑你只想要:

String ns = "urn:iso:std:iso:20022:tech:xsd:" + SepaSchemaUtils.SepaSchemaToString(schema);
var xml = new XmlDocument();
xml.AppendChild(xml.CreateXmlDeclaration("1.0","yes"));
var el = (XmlElement) xml.AppendChild(xml.CreateElement("Document",ns)); 
el.SetAttribute("xmlns",ns);

或者,我强烈建议使用LINQ to XML,这使得这个和许多其他任务变得更加简单.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读