在 Silverlight 中自动将 XML 反序列化为类集合?
抱歉,如果这本质上是通用的,但我有一个问题,可能与我对 .NET 和 Silverlight 的一些核心底层规则缺乏了解有关。
我目前有一个基本项目,很简单:
一个 ASP.NET 通用处理程序写出 XML
在 Silverlight 中,我使用 WebClient 对象来获取XML 输出,事实上我 已经浏览过 Scott Gu 的教程: Scott Gu 的优秀教程
我使用了 LINQ TO XML 的示例,本质上是将 XML 加载到类集合中。例如:
XDocument saleslogdata = XDocument.Parse(e.Result);
var logrecords = from data in saleslogdata.Descendants("data")
where data.Element("logID") != null
select new SalesLog
{
logID = (int)data.Element("logID"),
name = (string)data.Element("name"),
};
grdSalesLog.ItemsSource = logrecords;
类如下(只是一个基本示例):
public class SalesLog
{
public int logID { get; set; }
public string name { get; set; }
}
所以我的主要问题是......
有没有一种方法可以“自动生成”上面示例的类构建部分。 换句话说,如果我需要向源 XML 添加另外 10 列,我显然需要进入我的 silverlight 应用程序,更改上面的代码以扩展类以合并新字段,当然还有要映射的 LINQ to XML 查询类的新 XML 字段。
再说一次,我确信我在这里遗漏了一些基本的东西!
Sorry if this is generic in nature, but I have a question that maybe is related to my lack of understanding of some core underlying rules of .NET and Silverlight.
I have a basic project at the moment that simply:
An ASP.NET generic handler writes out XML
Within Silverlight, I am using the WebClient object to get the XML output, in fact I
have been running through Scott Gu's tutorial:
Scott Gu's excellent tutorial
I have used the example of LINQ TO XML to essentially load the XML into a class collection.. for example:
XDocument saleslogdata = XDocument.Parse(e.Result);
var logrecords = from data in saleslogdata.Descendants("data")
where data.Element("logID") != null
select new SalesLog
{
logID = (int)data.Element("logID"),
name = (string)data.Element("name"),
};
grdSalesLog.ItemsSource = logrecords;
The class is as follows (just a basic example):
public class SalesLog
{
public int logID { get; set; }
public string name { get; set; }
}
SO MY MAIN QUESTION IS THIS...
Is there a way I can "auto generate" the class building part of the above example. In otherwords say if I need to add another 10 columns to the source XML, I obviously need to go into my silverlight app, change the above code to expand the class to incorporate the new fields, and of course the LINQ to XML query to map the new XML fields to the class.
Again, I am sure I am missing something fundemental here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看文章 http://mironabramson.com/blog/post/2008/06/Create-you-own-new-Type-and-use-it-on-run-time-( C).aspx。
它解释了如何在运行时创建类型和实例。
我想动态类型会让 C# 4.0 中的事情变得更加容易。
Check out the article http://mironabramson.com/blog/post/2008/06/Create-you-own-new-Type-and-use-it-on-run-time-(C).aspx.
It explains how to create a type and an instance at runtime.
And I guess dynamic types will make it more easier in C# 4.0.