如何指定XML序列化反序列化属性在.NET中支持空间prefixes? [英] How do I specify XML serialization attributes to support namespace prefixes during deserialization in .NET?

查看:253
本文介绍了如何指定XML序列化反序列化属性在.NET中支持空间prefixes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的XML:

<person xmlns:a="http://example.com" xmlns:b="http://sample.net">
    <a:fName>John</a:fName>
    <a:lName>Wayne</a:lName>
    <b:age>37</b:age>
</person>

如何定义一个类XML序列化的属性,以支持描述的场景?

How do I define XML serialization attributes on a class to support described scenario?

推荐答案

您将需要指出哪些通过使用的XmlElement 属性。这将允许你到现场与特定的命名空间相关联,但你还需要暴露在你的类属性,它返回类型<一href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlnamespacedeclarationsattribute%28VS.80%29.aspx">XmlNamespaceDeclarations为了得到preFIX协会

You'll need to indicate which namespaces each field requires by using Namespace of XmlElement attribute. This will allow you to associate a field with a particular namespace, but you'll also need to expose a property on your class that returns type XmlNamespaceDeclarations in order to get the prefix association.

请参阅下面的文档和示例:

See documentation and sample below:

 [XmlRoot(ElementName="person")]
    public class Person
    {

        [XmlElement(Namespace="http://example.com")]
        public string fname;

        [XmlElement(Namespace="http://sample.com")]
        public string lname;

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();

        public Person()
        {
            xmlns.Add("a", "http://example.com");
            xmlns.Add("b", "http://sample.com");
        }
    }

这篇关于如何指定XML序列化反序列化属性在.NET中支持空间prefixes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆