Displaying xml response to a class?

I don't know how to represent some XML as a C # class. Does anyone have any suggestions as to how to display this xml correctly? 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; }
}

      

+3


source to share


4 answers


You can 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, the class authenticationResponse

is redundant.



If you have the answer in memory (not in a file on your hard drive), you can use this:

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

      

+3


source


Visual Studio 2012 has this cool feature called Paste XML As Classes (under Edit> Paste Special). You can just copy the XML to the clipboard and this "Paste XML As Classes" function will generate and paste this class authenticationResponse

for you:



/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class authenticationResponse
{

    private byte[] accountsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute()]
    [System.Xml.Serialization.XmlArrayItemAttribute("AccountId", IsNullable = false)]
    public byte[] Accounts
    {
        get
        {
            return this.accountsField;
        }
        set
        {
            this.accountsField = value;
        }
    }
}

      

+5


source


You can deserialize XML in the following class:

[XmlRoot("authenticationResponse")] 
public class AuthenticationResponse 
{ 
    [XmlArrayItem("AccountId")] 
    public List<long> Accounts { get; set; } 
}

      

Here's the code to deserialize:

AuthenticationResponse response = null; 
var serializer = new XmlSerializer(typeof(AuthenticationResponse));

using (StringReader sr = new StringReader(xml)) 
{ 
    response = (AuthenticationResponse)serializer.Deserialize(sr); 
}

      

+3


source


I am not using Visual Studio 2012 so I don't have Paste XML class as Class. However, in situations like this, if I need a quick solution, I often use the xsd.exe program in Visual Studio Tools. It generates a C # class from an XML schema definition (.xsd file).

If you don't have an XSD for the XML in question, you can quickly generate one from several XML tools. I'm using oXygen (trial available), download the XML sample and select Tools | Generate / Transform Schema. Long term, assuming you don't want to rely on third party tools, I would insist that the XML datasource provided me as well scheme.

Sample xsd.exe command line (run from the Visual Studio command line):

    xsd.exe FileName.xsd /n:Namespace.Cust.App.UI /c

      

will create a .cs file named FileName.cs.

+2


source







All Articles