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>
任何可能的方式来做到这一点?
*发布后进行少量修改。
你的意思namespace.Add(的String.Empty”文件“);? – TrN 2012-02-17 14:18:09
这是一样的东西。”“或String.Empty。 – 2012-02-17 14:19:51
但我现在正在做,但命名空间不可见。 – TrN 2012-02-17 14:25:14