关于C#输出xml的问题

.Net技术 码拜 8年前 (2016-09-26) 1771次浏览
再一次来发帖求帮助……本人希望通过c#可以输出入上面的xml,搜了几个例子依样画瓢了一下,生成了下面的xml,发现有很多地方不对。希望高手能帮本人指正一下。主要是在使用SetAttirbute的时候,rdf:ID  之类的输出不正确。请高手帮本人看一下。关于C#输出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", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub1.SetAttribute("rdf:ID", "_28159176D77E2467");
            xmlele.AppendChild(xmlsub1);
            XmlElement xmlsub1name = xmlDoc.CreateElement("cim", "Naming.name", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub1name.InnerText = "WALDEN";
            xmlsub1.AppendChild(xmlsub1name);
            XmlElement xmlsub1member = xmlDoc.CreateElement("cim", "Substation.MemberofSubControlArea", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub1member.SetAttribute("rdf:resource", "#55D3DE366B2AD032");
            xmlsub1.AppendChild(xmlsub1member);
            XmlElement xmlsub2 = xmlDoc.CreateElement("cim", "EnergConsumer", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub2.SetAttribute("rdf:ID", "_963867E4A4B1699");
            xmlele.AppendChild(xmlsub2);
            XmlElement xmlsub2pfixed = xmlDoc.CreateElement("cim", "EnergyConsumer.Pfixed", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub2pfixed.InnerText = "89.59";
            xmlsub2.AppendChild(xmlsub2pfixed);
            XmlElement xmlsub2qfixed = xmlDoc.CreateElement("cim", "EnergyConsumer.Qfixed", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub2qfixed.InnerText = "20.42";
            xmlsub2.AppendChild(xmlsub2qfixed);
            XmlElement xmlsub2load = xmlDoc.CreateElement("cim", "EnergyConsumer.LoadArea", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub2load.SetAttribute("rdf:resouce", "#_9C1602456V178B75");
            xmlsub2.AppendChild(xmlsub2load);
            XmlElement xmlsub2terminal = xmlDoc.CreateElement("cim", "ConductingEquipment.Terminal", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub2terminal.SetAttribute("rdf:resource", "#_A920F93C9D23DD45");
            xmlsub2.AppendChild(xmlsub2terminal);
            XmlElement xmlsub2member = xmlDoc.CreateElement("cim", "Equipment.Memberof_EquipmentContainer", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub2member.SetAttribute("rdf:resource", "#_A9D1427B3784CD78");
            xmlsub2.AppendChild(xmlsub2member);
            XmlElement xmlsub2name=xmlDoc.CreateElement("cim", "Naming.name", "http://iec.ch/TC57/2003/CIM-schema-cim10#");
            xmlsub2name.InnerText="610";
            xmlsub2.AppendChild(xmlsub2name);
            xmlDoc.Save("test1.xml");
解决方案

40

xmlsub1.SetAttribute(“ID”, “http://www.w3.org/1999/02/22-rdf-syntax-ns#”, “_28159176D77E2467″);
原理解析:
<rdf:RDF xml:base=”nari” xmlns:rdf=”http://www.w3.org/1999/02/22-rdf-syntax-ns#”
开头有设置rdf命名空间,则后面SetAttribute时,会寻找已有的命名空间,假如存在,则把别名放在前缀,例如 rdf:
否则会变成
<cim:Substation d2p1:ID=”_28159176D77E2467″ rdf:ID1=”111111111111111″ xmlns:d2p1=”未引入的命名空间”>
本人感受下吧。

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