简体   繁体   中英

Linq2XML Syntax for adding to an array

I actually have a working code for this but wanted to learn the other way to get the same answer and am stuck

Working Code:

var responseElement = XElement.Load("QuoteResponse.xml");
var myobject = new QuoteResponse
{
   response = new Response
   {
      id = (string)responseElement.Attribute("id"),
      quotes = new Quotes
      {
         quote = (
         from o in responseElement.Elements("quotes").Elements("quote")
         select new Quote
         {
             ask = (string)o.Element("ask")
         }).ToArray()
      }
   }
};

Alternative solution (am stuck with last piece - adding new Quote objects to Quote []

var responseElement = XElement.Load("QuoteResponse.xml");
var quoteElement = responseElement.Element("quote");
var myobject = new QuoteResponse
{
    response = new QR.Response
    {
       id = (string)responseElement.Attribute("id"),
       quotes = new Quotes
       {
          quote = new Quote[]
          {
              //Need to do something here so I can basically 
              //add each instance of new Quote to the array 
              new Quote
              {
                  ...
              }
          }
       }
    }
};

My class schema - FYI

 public class QuoteResponse
{
    public Response response { get; set; }
}

public class Response
{
    public string @id { get; set; }
    public Quotes quotes { get; set; }
}

public class Quotes
{
    public Quote[] quote { get; set; }
}

public class Quote
{
    public string ask { get; set; }
}

Any code snippet/pointers will really help

var allQuotes = responseElement.Elements("quotes").Elements("quote").ToArray();

quotes = new Quotes[]
  {
     new Quote
     {
         ask = (string)allQuotes[0].Element("ask")
     },
     new Quote
     {
         ask = (string)allQuotes[1].Element("ask")
     },
     new Quote
     {
         ask = (string)allQuotes[2].Element("ask")
     }, ...
    // happy guessing what the last index will be..
  }

I think you are looking for collection initialisation with object initialisation.

Something like this perhaps:

quote = new Quote[]
{
    new Quote {        
        ask = "something"
    },
    new Quote {        
        ask = "something else"
    }
}

But you cannot use that with the unknown amount of quote elements. The best and most elegant way is to use your working version.

One thing you should consider though, is to change the case of you public properties into this.

public class QuoteResponse
{
    public Response Response { get; set; }
}

public class Response
{
    public string Id { get; set; }
    public IEnumerable<Quote> Quotes { get; set; }
}

// Let's get rid of this class altogether, use IEnumerable<Quote> instead
public class Quotes
{
    public Quote[] Quote { get; set; }
}

public class Quote
{
    public string Ask { get; set; }
}

You are, of course, free to use whatever format you want. But with your current formatting you are actively hurting most C# developers :)

Read about Microsoft's recommendations: http://msdn.microsoft.com/en-us/library/ms229012.aspx

And here is about the IEnumerable<T> thing: http://msdn.microsoft.com/en-us/library/9eekhta0.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM