映射XML响应上课吗? [英] mapping an xml response to class?

查看:134
本文介绍了映射XML响应上课吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何重新present一些XML作为一个C#类。没有任何人有任何建议,如何正确映射此XML?这里是我下面的尝试:

 < authenticationResponse>
  <帐户>
    < ACCOUNTID> 1 LT; / ACCOUNTID>
    <&ACCOUNTID GT; 5℃/ ACCOUNTID>
  < /帐户>
< / authenticationResponse>
公共类authenticationResponse
{
    [的XmlElement(帐户)]
    [数据成员]
    公开名单<&帐户GT;帐户{搞定;组; }
}公共类帐户
{
    众长ID {搞定;组; }
}


解决方案

您可以通过LINQ加载这些数据的XML:

 的XElement X = XElement.Load(YourFile.xml);
清单<&帐户GT;账户= x.Element(帐户)
                            .Elements(帐户ID)
                            。选择(E =>新建帐户{ID =(长)电子})
                            .ToList();

在这种情况下, authenticationResponse 类是多余的。

如果你有在内存中(而不是在你的硬盘文件)的响应,你可以使用这样的:

 字符串响应= ...
的XElement X = XElement.Load(新StringReader(响应));

I don't know how to represent some XML as a C# class. Does anyone have any suggestions as to how to properly map this xml? Here's my attempt below:

<authenticationResponse>
  <Accounts>
    <AccountId>1</AccountId>
    <AccountId>5</AccountId>
  </Accounts>
</authenticationResponse>


public class authenticationResponse
{
    [XmlElement("Accounts")]
    [DataMember]
    public List<Account> Accounts { get; set; }
}

public class Account
{
    public long id { get; set; }
}

解决方案

You may load this data via LINQ to XML:

XElement x = XElement.Load("YourFile.xml");
List<Account> accounts = x.Element("Accounts")
                            .Elements("AccountId")
                            .Select(e => new Account { id = (long)e })
                            .ToList();

In this case authenticationResponse class is redundant.

If you've got the response in memory (not in a file on your harddrive), you may use this:

string response = ...
XElement x = XElement.Load(new StringReader(response));

这篇关于映射XML响应上课吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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