如何从 XmlDocument 中的多个元素创建业务对象的集合
我正在尝试使用 .net 4.0/c# 从以下 xml 文档创建业务对象的集合,
<WebServices ErrorFound="False" ServerDateTime="30/11/2010 14:58:58">
<Results>
<Users TotalResults="5">
<UserName UserID="2">John</UserName>
<UserName UserID="3">Dave</UserName>
<UserName UserID="4">Jim</UserName>
<UserName UserID="5">Bob</UserName>
</Users>
</Results>
</WebServices>
这是我需要创建的类,
public class User
{
public string Name { get; set; }
public int ID { get; set; }
}
每个 Users 子元素都应该是该类的一个新实例。 到目前为止,我有这个接受 XmlDocument 的方法。
public static IEnumerable<User> GetUser(XmlDocument xmlDoc)
{
XmlReader reader = XmlReader.Create(new StringReader(xmlDoc.OuterXml));
XDocument doc = XDocument.Load(reader);
var user = from u in doc.Descendants("WebServices").DescendantsAndSelf("Users")
select new User()
{
Name = u.Element("UserName").Value,
ID = int.Parse(u.Element("UserName").Attribute("UserID").Value)
};
List<User> userInstance = user.ToList();
IEnumerable<User> users= from u in userInstance
select u;
return users;
}
就从第一个子元素生成对象的一个实例而言,这工作得很好,但我不确定如何从所有元素创建多个实例。 我需要能够返回 User 对象的集合,例如 Collection
我可能完全找错了树。非常感谢任何帮助。
I'm trying to create a collection of business objects from the following xml document using .net 4.0/c#
<WebServices ErrorFound="False" ServerDateTime="30/11/2010 14:58:58">
<Results>
<Users TotalResults="5">
<UserName UserID="2">John</UserName>
<UserName UserID="3">Dave</UserName>
<UserName UserID="4">Jim</UserName>
<UserName UserID="5">Bob</UserName>
</Users>
</Results>
</WebServices>
This is the class I need to create
public class User
{
public string Name { get; set; }
public int ID { get; set; }
}
Each of the Users child elements should be a new instance of the class.
So far I have this method which accepts an XmlDocument.
public static IEnumerable<User> GetUser(XmlDocument xmlDoc)
{
XmlReader reader = XmlReader.Create(new StringReader(xmlDoc.OuterXml));
XDocument doc = XDocument.Load(reader);
var user = from u in doc.Descendants("WebServices").DescendantsAndSelf("Users")
select new User()
{
Name = u.Element("UserName").Value,
ID = int.Parse(u.Element("UserName").Attribute("UserID").Value)
};
List<User> userInstance = user.ToList();
IEnumerable<User> users= from u in userInstance
select u;
return users;
}
This works fine as far as producing one instance of the object from the first child element is concerned but I am unsure as to how to create multiple instances from all the elements.
I need to be able to return a collection of the User objects eg Collection<User> users = new Collection<User>()
I could be barking up completely the wrong tree. Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 XPath 您可以编写如下代码:
Using XPath you can write the code like this:
好吧,我终于弄清楚我的代码出了什么问题。
现在我不再
有
这样的语句: doc.Descendants("UserName") 基本上生成一个可以迭代的 UserName 元素数组,然后我可以直接访问该元素的值并设置它到我的班级属性。
Ok, so I finally figured out what was wrong with my code.
Instead of
I now have
the statement:
doc.Descendants("UserName")
basically produces an array of the UserName elements which can be iterated through, I can then directly access the value of that element and set it to my class properties.