2012-02-17 22 views
0

我有我想序列類:XmlSerializer命名空間不可見,爲什麼?

[XmlRoot(ElementName = "FileInfoMessage")] 
public class Message : IMessage 
{ 
[XmlAttribute] 
public int Id { get; set; } 

[XmlAttribute(Namespace = "Files")] 
public Version { get; set; } 
//... 
} 

和我的序列化方法:

//_xmlData - string containing xml content 
public void SerializeData(IMessage message, Encoding encoding) 
     { 
      using (var stream = new MemoryStream()) 
      { 
       var writerSettings = new XmlWriterSettings {Encoding = encoding, OmitXmlDeclaration = false, Indent = true, NamespaceHandling = NamespaceHandling.Default }; 
       var writer = XmlWriter.Create(stream, writerSettings); 
       var serializer = new XmlSerializer(message.GetType()); 
       var namespaces = new XmlSerializerNamespaces(); 
       namespaces.Add("", "Files"); //if I add custom prefix, it will be visible 
       serializer.Serialize(writer, message, namespaces); 
       stream.Position = 0; 
       _xmlData = new StreamReader(stream).ReadToEnd(); 
      } 

什麼,我想才達到的是:

<?xml version="1.0" encoding="windows-1250"?> 
<FileInfoMessage Id = "1" **xmlns="Files"** Version="2"> 
//... 
</FileInfoMessage> 

,但我得到:

<?xml version="1.0" encoding="windows-1250"?> 
<FileInfoMessage Id = "1" p1:Version="2" xmlns:p1="Files"> 
//... 
</FileInfoMessage> 

,如果我更改我的SerilizeMethod行: namespaces.Add(「whateverIwant」,「Files」);

我得到:

<?xml version="1.0" encoding="windows-1250"?> 
    <FileInfoMessage Id = "1" **xmlns:whateverIwant="Files"** Version="2"> 
    //... 
    </FileInfoMessage> 

任何可能的方式來做到這一點?

*發佈後進行少量修改。

回答

0

嘗試傳遞一個空字符串的第一個參數namespaces.Add(「」,「文件)。至於我能記住這一點做的伎倆。

+0

你的意思namespace.Add(的String.Empty」文件「);? – TrN 2012-02-17 14:18:09

+0

這是一樣的東西。」「或String.Empty。 – 2012-02-17 14:19:51

+0

但我現在正在做,但命名空間不可見。 – TrN 2012-02-17 14:25:14

相關問題