xml中怎么添加noNamespaceSchemaLocation和xsi

阿尔萨斯djw 2014-10-16 10:01:09
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-
envelope.xsd">

XmlDocument xmldoc = new XmlDocument();
XmlElement xmlelem;
XmlElement data;
xmlelem = xmldoc.CreateElement("", "AmazonEnvelope", "");
//xmlelem.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
//xmlelem.SetAttribute("xsi","noNamespaceSchemaLocation", "amzn-envelope.xsd");
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XAttribute att = new XAttribute(xsi + "noNamespaceSchemaLocation", "amzn-envelope.xsd") ;

我直接用setAttribute不行
网上有一个用XDocument的但是我用的是XmlDocument,不想改了
...全文
788 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SowLuo 2014-11-04
  • 打赏
  • 举报
回复

我已经找到方法了。。。顺便也告诉你下

// 首先要创建一个空的XML文档 ,然后在其中加入元素
                    XmlDocument doc = new XmlDocument();
                    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                    XmlAttribute xsispace = doc.CreateAttribute("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
                    xsispace.Value = "Schema-3.0.xsd";
                    XmlAttribute xsi = doc.CreateAttribute("xmlns:xsi");
                    xsi.Value = "http://www.w3.org/2001/XMLSchema-instance";

                    doc.AppendChild(dec);
                    //创建一个根节点(Document)
                    XmlElement Document = doc.CreateElement("Document");
                    Document.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
                    Document.Attributes.Append(xsi);
                    Document.Attributes.Append(xsispace); 
                    Document.SetAttribute("SN", "323232323");
                    Document.SetAttribute("Version", "3.0");
                    Document.SetAttribute("License", "00120-100290-002");
                    doc.AppendChild(Document);
SowLuo 2014-11-04
  • 打赏
  • 举报
回复
引用 3 楼 u014088573 的回复:
今天论坛都潜水了吗
请问楼主 你这个问题处理了吗? 我也遇到了。 如果有方法了 麻烦告知下!!谢谢
save4me 2014-10-16
  • 打赏
  • 举报
回复
参考: Creating attributes for root element

 XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("AmazonEnvelope");
            root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            root.SetAttribute("noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "amzn-envelope.xsd");
            doc.AppendChild(root);
            doc.Save("TestEE.xml");
阿尔萨斯djw 2014-10-16
  • 打赏
  • 举报
回复
今天论坛都潜水了吗
阿尔萨斯djw 2014-10-16
  • 打赏
  • 举报
回复
引用 1 楼 save4me 的回复:
参考: Creating attributes for root element

 XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("AmazonEnvelope");
            root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            root.SetAttribute("noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "amzn-envelope.xsd");
            doc.AppendChild(root);
            doc.Save("TestEE.xml");
这样的结果是<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="amzn-envelope.xsd"> 不是我想要的 <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn- envelope.xsd">

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧