【问题标题】:Generate xml attributes生成xml属性
【发布时间】:2012-05-23 23:25:45
【问题描述】:

我必须创建一个 xml 文件,其中包含具有以下属性的元素:

 <element 
     xsi:schemaLocation="http://test.xsd" 
     xmlns="http://test2" 
     xmlns:xsi=http://test3>

我试过了:

 XNamespace ns = "xsi";            
 var root = new XElement("element",
                       new XAttribute(ns + "schemaLocation", "http://test.xsd"), // (I)
                       new XAttribute(XNamespace.Xmlns, "http://test2"),         // (II)
                       new XAttribute(XNamespace.Xmlns + "xsi", "http://test3"), // (III)

但唯一生成好的是(III):

 xmlns:xsi=http://test3

(I) 生成如下:

 p1:schemaLocation="http://test.xsd" xmlns:p1="xsi"

并且 (II) 没有生成,因为该行没有编译。

知道如何生成这些属性吗?

谢谢你, 升

编辑 - 也在这里找到它:Creating XML with namespaces and schemas from an XElement

【问题讨论】:

    标签: c# xml namespaces xelement xattribute


    【解决方案1】:
    const string ns = "http://test2";
    const string si = "http://test3";
    const string schema_location = "http://test.xsd";
    
    XNamespace xns = ns;
    XNamespace sinsp = si;
    
         XElement xe = new XElement(xns + "element",
               new XAttribute(XNamespace.Xmlns + "xsi", si),
               new XAttribute(sinsp+ "schemaLocation", schema_location),
               new XElement(xns + "sometag", "somecontent")
            );
    
         return xe;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2016-01-27
      相关资源
      最近更新 更多