2014-01-27 84 views
-2

我有一個對象,我想序列化它。我想將名稱空間添加到xml文檔的特定元素。我從1個默認的xml創建了幾個.xsd文件。我使用XmlSerializer。xmlserializernamespaces將命名空間添加到特定元素

命名空間應該在中描述。這就是我想要的:

<env:root 
    xmls:env ="httpenv" 
    xmlns:sos="httpsos"> 
    <env:body> 
    <sos:element 
     xmlns:abc="" <--------------my namespaces are located in <sos:element 
     ... 

如果我使用類似

XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
ns.Add("abc", "httpabc"); 
ns.add.... 
StringWriter stringWriter = new StringWriter(); 
serializer.Serialize(stringWriter, ObjectToSerialize, ns); 

我會得到下面的

<env:root 
    xmls:env ="httpenv" 
    xmlns:sos="httpsos" 
    xmlns:abc="" <-------------I do not want it here; I want it in <sos:element 
    <env:body> 
    <sos:element> 
     ... 

是否有指定的方式(以哪個元素)我想要聲明我的名稱空間還是全部在根元素中聲明?

+0

如何是XSD相關的回答你的問題的文件嗎? –

+0

提及xsd文件,只是一個小sideinfo。順便說一句,爲什麼我得到一個downvote .. – Gero

回答

0

從XML的角度來看,定義XML名稱空間的位置並不重要。如果您需要在特定位置使用XML名稱空間聲明,那麼解析XML的組件可能有問題。

好了,反正這是我想出了:

using System; 
using System.IO; 
using System.Xml; 
using System.Xml.Serialization; 

namespace XMLNamespaceChangeSerialization 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      var serialize = Serialize(); 
      Console.WriteLine(serialize); 
      Console.ReadLine(); 
     } 

     private static string Serialize() 
     { 
      var ns = new XmlSerializerNamespaces(); 
      ns.Add("env", "httpenv"); 
      // Don't add it here, otherwise it will be defined at the root element 
      // ns.Add("sos", "httpsos"); 
      var stringWriter = new StringWriter(); 
      var serializer = new XmlSerializer(typeof (RootClass), "httpenv"); 
      serializer.Serialize(stringWriter, new RootClass(), ns); 
      return stringWriter.ToString(); 
     } 
    } 


    [Serializable] 
    [XmlRoot(ElementName = "root")] 
    public class RootClass 
    { 
     [XmlElement(ElementName = "body", Namespace = "httpenv")] 
     public BodyClass body = new BodyClass(); 
    } 

    [Serializable] 
    public class BodyClass 
    { 
     [XmlElement(ElementName = "element", Namespace = "httpsos")] 
     public SOSClass element = new SOSClass(); 
    } 

    [Serializable] 
    public class SOSClass 
    { 
     // This will be used by XML serializer to determine the namespaces 
     [XmlNamespaceDeclarations] 
     public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(
        new[] { new XmlQualifiedName("sos", "httpsos"), }); 
    } 
} 
+1

只需要:''ns.Add(「sos」,「httpsos」);''到你的''Serialize()''。和 在你的SOSClass中更改''public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(new [] {new XmlQualifiedName(「sos」,「httpsos」),});''public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(new [] {new XmlQualifiedName(「abc」,「WriteNamespaceHere」),});''你完成了 – eyalsn

+0

@eyalsn:謝謝你的提示。的確,我解釋瞭如何將'sos'命名空間移到別的地方。有了它背後的概念,讀者現在應該能夠在任何地方移動任何名字空間。 –

+0

@ThomasWeller我可以做什麼你沒有編輯原始類顯示?因爲我的類是由svcUtil使用wsdl定義生成的,我不想觸摸它,而是我想操作Serializer對象,這樣當它串行化時,它會向特定元素添加前綴/名稱空間。這可能嗎 ? – joedotnot

相關問題