gpt4 book ai didi

c# - 将 XML 解析为对象

转载 作者:太空宇宙 更新时间:2023-11-03 11:01:57 26 4
gpt4 key购买 nike

这是 XML:

  <?xml version="1.0" encoding="utf-8" ?> 
<object>
<body>tests</body>
<send_results type="null" />
<note_class>none</note_class>
<users type="list" />
<title>test</title>
<time_sent type="null" />
<image type="null" />
<to_customers type="boolean">False</to_customers>
<time_created>2013-06-26T16:40:50</time_created>
<num_sends type="integer">0</num_sends>
<time_scheduled type="null" />
<dealership>/api/v1/dealerships/10/</dealership>
<id type="integer">22</id>
<send_rate>none</send_rate>
<send_method>email</send_method>
<reply_to_email>do_not_reply@williamson-cadillac.com</reply_to_email>
<response_link type="null" />
<to_users type="boolean">True</to_users>
<resource_uri>/api/v1/notifications/22/</resource_uri>
</object>

我正在尝试将 Xml 解析为一个对象...

       var query = from m in parsedXml.Decendants("object")
select new Notifications
{
Body = (string)m.Element("body"),
SendResults = (string)m.Element("send_results"),
NoteClass = (string)m.Element("note_class"),
Users = m.Element("users").Elements().Select(e => (string)e.Element("value")).ToList(),
Title = (string)m.Element("title"),
TimeSent = (string)m.Element("time_sent"),
Image = (string)m.Element("image"),
ToCustomers = (string)m.Element("to_customers"),
TimeCreated = (string)m.Element("time_created"),
NumSends = (string)m.Element("num_sends"),
TimeScheduled = (string)m.Element("time_scheduled"),
Dealership = (string)m.Element("dealership"),
Id = (string)m.Element("id"),
SendRate = (string)m.Element("send_rate"),
SendMethod = (string)m.Element("send_method"),
ReplyToEmail = (string)m.Element("reply_to_email"),
ResponseLink = (string)m.Element("response_link"),
ToUsers = (string)m.Element("to_users"),
ResourceUri = (string)m.Element("resource_uri"),
};

我一直在变量“query”中得到“nothing”或“null”。

我想不通 - 我尝试了很多不同的方法。感谢您在这些问题上的帮助。

最佳答案

我复制了您的代码并将其直接粘贴到控制台应用程序中。它完全按预期工作。

enter image description here

您还有什么可以向我们展示的可能对结果有影响的东西吗?

这是我使用的完整示例,以防你做一些不同的事情......

string xml = 
@"<?xml version='1.0' encoding='utf-8' ?>
<object>
<body>tests</body>
<send_results type='null' />
<note_class>none</note_class>
<users type='list' />
<title>test</title>
<time_sent type='null' />
<image type='null' />
<to_customers type='boolean'>False</to_customers>
<time_created>2013-06-26T16:40:50</time_created>
<num_sends type='integer'>0</num_sends>
<time_scheduled type='null' />
<dealership>/api/v1/dealerships/10/</dealership>
<id type='integer'>22</id>
<send_rate>none</send_rate>
<send_method>email</send_method>
<reply_to_email>do_not_reply@williamson-cadillac.com</reply_to_email>
<response_link type='null' />
<to_users type='boolean'>True</to_users>
<resource_uri>/api/v1/notifications/22/</resource_uri>
</object>";
var parsedXml = XDocument.Parse(xml);
var query = from m in parsedXml.Descendants("object")
select new Notifications
{
Body = (string)m.Element("body"),
SendResults = (string)m.Element("send_results"),
NoteClass = (string)m.Element("note_class"),
Users = m.Element("users").Elements().Select(e => (string)e.Element("value")).ToList(),
Title = (string)m.Element("title"),
TimeSent = (string)m.Element("time_sent"),
Image = (string)m.Element("image"),
ToCustomers = (string)m.Element("to_customers"),
TimeCreated = (string)m.Element("time_created"),
NumSends = (string)m.Element("num_sends"),
TimeScheduled = (string)m.Element("time_scheduled"),
Dealership = (string)m.Element("dealership"),
Id = (string)m.Element("id"),
SendRate = (string)m.Element("send_rate"),
SendMethod = (string)m.Element("send_method"),
ReplyToEmail = (string)m.Element("reply_to_email"),
ResponseLink = (string)m.Element("response_link"),
ToUsers = (string)m.Element("to_users"),
ResourceUri = (string)m.Element("resource_uri"),
};

Console.WriteLine("Number of items = {0}, {1}",query.Count(), query.FirstOrDefault().Body);
Console.ReadLine();

关于c# - 将 XML 解析为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com