2017-03-03 125 views
0

我想在Xml序列化期間添加命名空間。到目前爲止,我已經添加了一些,但我無法使用Xml序列化程序添加自定義名稱空間。C#Xml序列化屬性添加自定義命名空間

到目前爲止,我已經實現了這個

<?xml version="1.0"?> 
<manifest xmlns:xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2e" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xs" identifier="test" version="2"> 
    <lom xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="test lom" xmlns="http://ltsc.ieee.org/xsd/LOM" /> 
    <resource adlcp:scormtype="sco" /> 
</manifest> 

這是我的代碼

Manifest類

[Serializable] 
[XmlRoot(ElementName = "manifest")] 
public class Manifest 
{ 
    /// <summary> 
    /// manifest root node 
    /// </summary> 
    [XmlAttribute(AttributeName = "identifier")] 
    public string Identifier { get; set; } 

    [XmlAttribute(AttributeName = "version")] 
    public string Version { get; set; } 

    //[XmlAttribute(AttributeName = "adlcp", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] 
    //public string Adlcp = "http://www.adlnet.org/xsd/adlcp_rootv1p2"; 

    [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] 
    public string SchemaLocation = "http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xs"; 


    [XmlElement(ElementName = "lom", Namespace = "http://ltsc.ieee.org/xsd/LOM")] 
    public Lom Lom { get; set; } 

    [XmlElement("resource")] 
    public Resource Resource { get; set; } 
} 

洛美類

public class Lom 
    { 
     [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] 
     public string SchemaLocation { get; set; } 

     [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.imsproject.org/xsd/imscp_rootv1p1p2e")] 
     public string Xsi = "http://www.w3.org/2001/XMLSchema-instance"; 


    } 

資源類這樣

using (FileStream fs = new FileStream(filePath, FileMode.Create)) 
     { 
      var xmlSerializer = new XmlSerializer(typeof(Manifest)); 

     var ns = new XmlSerializerNamespaces(); 
     ns.Add("xmlns", "http://www.imsproject.org/xsd/imscp_rootv1p1p2e"); 
     ns.Add("adlcp", "http://www.adlnet.org/xsd/adlcp_rootv1p2"); 
     ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
     xmlSerializer.Serialize(fs, data, ns); 

     fs.Close(); 

    } 
public class Resource 
    { 
     [XmlAttribute(AttributeName = "scormtype", Namespace = "http://www.adlnet.org/xsd/adlcp_rootv1p2")] 
     public string ScormType { get; set; } 
    } 

我的序列化功能,我需要這樣的輸出。

<?xml version="1.0"?> 
    <manifest 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    identifier="test" version="2" 
    xmlns:adlcp="test" 
    xsi:schemaLocation="test location" 
    **xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"**> 
     <lom 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://ltsc.ieee.org/xsd/LOM lom.xsd" 
     xmlns="http://ltsc.ieee.org/xsd/LOM" /> 
    <resource adlcp:scormtype="sco"/> 
    </manifest> 

我做錯了什麼?

+0

如何實現所需的輸出? –

+0

你確實需要'lom'子元素上重複的xsi命名空間聲明嗎?我認爲這部分是很難做到的,沒有自定義序列化,因爲聲明是不必要的(它已經在父代聲明瞭)。 – steve16351

+0

你需要的最直接的事情是'[XmlRoot(ElementName =「manifest」,Namespace =「http://www.imsproject.org/xsd/imscp_rootv1p1p2」)]'? [XmlElement(「resource」)]''應該可能有一個明確的'Namespace' –

回答

1

只需添加一個namspace串行

  var xmlSerializer = new XmlSerializer(typeof(Manifest), "http://www.w3.org/2001/XMLSchema-instance"); 
      var ns = new XmlSerializerNamespaces(); 
      ns.Add("xmlns", "http://www.w3.org/2001/XMLSchema-instance"); 
      xmlSerializer.Serialize(fs, data, ns); 

,如果數據與屬性

[XmlAttribute(AttributeName = "adlcp", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] 
    public string Adlcp { get; set; } 

定義爲

 Manifest data = new Manifest() 
     { Identifier = "test", Version = "2", Adlcp = "test", SchemaLocation = "test location" }; 
     data.Resource = new Resource() { ScormType="sco" }; 

你會在輸出中獲得xmlns:adlcp="test"

[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] 
    public string Xsi = "http://www.w3.org/2001/XMLSchema-instance"; 

同樣的事情,以及

  ns.Add("adlcp", "http://www.imsproject.org/xsd/Manifest/Adlcp"); 

[XmlAttribute(AttributeName = "scormtype", Namespace = "http://www.imsproject.org/xsd/Manifest/Adlcp")] 
    public string ScormType { get; set; } 

得到一個可能的adlcp:scormtype="sco"

命名空間添加到根類也一樣,例如

[XmlRoot(ElementName = "manifest", Namespace = "http://www.imsproject.org/xsd/Manifest")] 
public class Manifest 
+0

我正在嘗試這個,並且會更新你 –

+0

我試過你的代碼,除了一個命名空間外,它完美的工作。我得到兩個xmlns,像這個xmlns:xmlns –

+0

我已經更新了我的問題 –