关于XML格式输出问题

.Net技术 码拜 8年前 (2016-09-26) 1502次浏览
第一次接触XML ,本人希望通过c#可以输出入上面的xml,搜了几个例子依样画瓢了一下,生成了下面的xml,发现有很多地方不对。希望高手能帮本人指正一下。主要错误是本人图中画线的地方。问题错误可能都比较低级,求轻喷……
以下是本人的代码

XmlDataDocument xmlDoc;
            xmlDoc = new XmlDataDocument();
            XmlDeclaration xmlDocNode = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
            xmlDoc.AppendChild(xmlDocNode);     //加入xml声明段落
            XmlElement xmlele = xmlDoc.CreateElement("rdf", "RDF", "rdf:RDF");
            xmlele.SetAttribute("xml:base", "nari");
            xmlele.SetAttribute("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
            xmlele.SetAttribute("xmlns:cim", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlele.SetAttribute("xmlns:cimNARI", "http://www.naritech.cn/CIM/ext-schema#");
            xmlDoc.AppendChild(xmlele);
            XmlElement xmlsub1 = xmlDoc.CreateElement("cim", "Substation", "cim:Substation");
            xmlsub1.SetAttribute("rdf:ID", "_28159176D77E2467");
            xmlele.AppendChild(xmlsub1);
            XmlElement xmlsub1name = xmlDoc.CreateElement("cim", "Naming.name", "cim:Naming.name");
            xmlsub1name.InnerText = "WALDEN";
            xmlsub1.AppendChild(xmlsub1name);
            XmlElement xmlsub1member = xmlDoc.CreateElement("cim", "Substation.MemberofSubControlArea", "cim:Substation.MemberofSubControlArea");
            xmlsub1member.SetAttribute("rdf:resource", "#55D3DE366B2AD032");
            xmlsub1.AppendChild(xmlsub1member);
            XmlElement xmlsub2 = xmlDoc.CreateElement("cim", "EnergConsumer", "cim:EnergyConsumer");
            xmlsub2.SetAttribute("rdf:ID", "_963867E4A4B1699");
            xmlele.AppendChild(xmlsub2);
            XmlElement xmlsub2pfixed = xmlDoc.CreateElement("cim", "EnergyConsumer.Pfixed", "cim:EnergyConsumer.Pfixed");
            xmlsub2pfixed.InnerText = "89.59";
            xmlsub2.AppendChild(xmlsub2pfixed);
            XmlElement xmlsub2qfixed = xmlDoc.CreateElement("cim", "EnergyConsumer.Qfixed", "cim:EnergyConsumer.Qfixed");
            xmlsub2qfixed.InnerText = "20.42";
            xmlsub2.AppendChild(xmlsub2qfixed);
            XmlElement xmlsub2load = xmlDoc.CreateElement("cim", "EnergyConsumer.LoadArea", "cim:EnergyConsumer.LoadArea");
            xmlsub2load.SetAttribute("rdf:resouce", "#_9C1602456V178B75");
            xmlsub2.AppendChild(xmlsub2load);
            xmlDoc.Save("test1.xml");
解决方案

40

Xml的命名空间是严格的,因此同一个命名空间,要用同一个URI
cim的URI是”http://iec.ch/TC57/2003/CIM-schema-cim10#”,那么,cim:Substation就要使用同一个URI。
红字部分就是要更改的地方,改成和蓝字一样:
xmlele.SetAttribute(“xmlns:cim”, “http://iec.ch/TC57/2003/CIM-schema-cim10#”);
XmlElement xmlsub1 = xmlDoc.CreateElement(“cim”, “Substation”, “cim:Substation”);
其余类推。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于XML格式输出问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)