我实际上有一个工作代码,但想学习另一种方法来获得相同的答案并被卡住
工作代码:
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()
}
}
};
替代解决方案(我坚持最后一块 - 将新的 Quote 对象添加到 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
{
...
}
}
}
}
};
我的班级模式 - 仅供参考
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; }
}
任何代码片段/指针都会有帮助