gpt4 book ai didi

c# - Linq 到 XML 转换器

转载 作者:行者123 更新时间:2023-11-30 20:54:26 24 4
gpt4 key购买 nike

有人可以帮助解决以下问题(我正在努力形成查询的地方)

XML

<?xml version="1.0" encoding="UTF-8"?>
<response id="1545346343">
<date>2013-10-01 12:01:55.532999</date>
<status>
<current>open</current>
<change_at>16:00:00</change_at>
</status>
<message>Market is open</message>
</response>

public class MarketClockResponse
{
public Response response { get; set; }
}
public class Response
{
public string Id { get; set; }
public string date { get; set; }
public Status status { get; set; }
public string message { get; set; }
}
public class Status
{
public string current { get; set; }
public string change_at { get; set; }
}

我的解决方案:

public void example3()
{
var xElem = XElement.Load("test.xml");

var myobject = xElem.Descendants("response").Select(
x => new MarketClockResponse
{
//Struggling to proceed from here
});
}

最佳答案

您正在尝试从 response 元素(它是您的 xml 的根)中选择 response 元素。直接使用这个元素:

var responseElement = XElement.Load(path_to_xml);
var statusElement = responseElement.Element("status");
var myobject = new MarketClockResponse
{
response = new Response
{
Id = (string)responseElement.Attribute("id"),
date = (string)responseElement.Element("date"),
message = (string)responseElement.Element("message"),
status = new Status
{
current = (string)statusElement.Element("current"),
change_at = (string)statusElement.Element("change_at")
}
}
};

关于c# - Linq 到 XML 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19139895/

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