如何将多个XML命名空间应用到同一类的XML序列化 [英] How to apply multiple xml namespaces to the same class with the XML Serializer

查看:131
本文介绍了如何将多个XML命名空间应用到同一类的XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成WCF代理类,当下面的类到下面的XML串行化,转换和实例。



然而,当我尝试命名空间应用到类,它们插入不正确,或根本没有。



我在做什么错了?我怎样才能解决这个问题。



提前很多感谢



类结构:?

  [XmlRoot] 
公共类请求
{
公众诠释标识

酒店的公共名称名称{;设置;}
}

[XmlRoot]
公共类名称
{
[XmlAttribute]
酒店的公共布尔测试{获取;集;}

公共字符串名字{获得;设置;}

公共字符串名字{获得;设置;}
}

期望中的XML结构的(多余的XML修剪)

 < X1:请求的xmlns:X1 =数据/主的xmlns:X2 =数据/全部 > 
< X2:ID> 0℃; / X2:ID>
< X2:姓名测试=真正的>
< X2:名字>&丹LT; / X2:名字>
< X2:名字>&阿特金森LT; / X2:名字>
< / X2:名称>
< / X1:请求>


解决方案

如果你正在谈论(XML)的属性(如<富巴=ABC/> ),那么你是不是在谈论的DataContractSerializer - 所以,或许坚持与 XmlType将等...是这样的:

  [XmlRoot (命名空间=数据/主)] 
公共类请求{
[XmlElement的空间(namespace =数据/全部)]
公众诠释标识{搞定;组; }
[XmlElement的空间(namespace =数据/全部)]
公共名称名称{;设置;}
}

[XmlType将(命名空间=数据/全部)]
公共类名称{
[XmlAttribute(测试)
公共BOOL测试{获取;集;}
公共字符串名字{获得;设置; }
公共字符串名字{获得;设置;}
}

这没有按'T有别名 - 但它似乎是正确的,至少...



合并上面锦衣卫的属性的 XmlSerializerNamespaces 代码,它应该在那里...



(信用这里锦衣卫,但是这是维基反正...)

 请求REQ =新的请求{
ID = 0,名称=新名称{
测试= TRUE,名字=丹,姓氏=阿特金森
}
};
XmlSerializerNamespaces NS =新XmlSerializerNamespaces();
ns.Add(×1,数据/主);
ns.Add(×2,数据/所有);
新的XmlSerializer(req.GetType())序列化(Console.Out,REQ,NS)。


I'm trying to generate a proxy class with WCF which, when serialized, converts and instance of the class below into the XML below.

However, when I try to apply namespaces to the classes, they're inserted incorrectly, or not at all.

What am I doing wrong? How can I fix this?

Many thanks in advance.

Class Structure:

[XmlRoot]
public class Request
{
  public int Id

  public Name Name {get;set;}
}

[XmlRoot]
public class Name
{
  [XmlAttribute]
  public bool test {get;set;}

  public string FirstName {get;set;}

  public string LastName {get;set;}
}

Desired XML structure (superfluous XML trimmed):

<x1:Request xmlns:x1="Data/Main" xmlns:x2="Data/All">
  <x2:Id>0</x2:Id>
  <x2:Name test="true">
    <x2:FirstName>Dan</x2:FirstName>
    <x2:LastName>Atkinson</x2:LastName>
  </x2:Name>
</x1:Request>

解决方案

If you are talking about (xml) attributes (i.e. <foo bar="abc"/>), then you aren't talking about DataContractSerializer - so perhaps stick with the XmlType etc... something like:

[XmlRoot(Namespace="Data/Main")]
public class Request {
  [XmlElement(Namespace = "Data/All")]
  public int Id { get; set; }
  [XmlElement(Namespace="Data/All")]
  public Name Name {get;set;}
}

[XmlType(Namespace="Data/All")]
public class Name {
  [XmlAttribute("test")]
  public bool Test {get;set;}
  public string FirstName {get;set;}
  public string LastName {get;set;}
}

That doesn't have the aliases - but it seems to be correct, at least...

Merge the attributes above with "Guard"'s XmlSerializerNamespaces code and it should be there...

(credit here to "Guard", but this is wiki anyway...)

    Request req = new Request {
        Id = 0, Name = new Name {
            Test = true, FirstName = "Dan", LastName = "Atkinson"
        }
    };
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("x1", "Data/Main");
    ns.Add("x2", "Data/All");
    new XmlSerializer(req.GetType()).Serialize(Console.Out, req,ns);

这篇关于如何将多个XML命名空间应用到同一类的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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