简体   繁体   English

Linq转XML转换器

[英]Linq to XML Converter

Can somebody please help with the below place (where I am struggling to form the query) 有人可以在以下地方提供帮助吗(我正在努力形成查询)

XML 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>

Class

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; }
}

My solution: 我的解决方案:

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

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

You are trying to select response elements from response element (which is root of your xml). 您试图从response元素(它是xml的根)中选择response元素。 Use this element directly instead: 直接使用此元素:

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")
        }
    }
};
var myobject = xElem.Descendants("response").Select(
        x => new MarketClockResponse
        {
              response = new Response
             {
                Id = x.Attribute("id").Value,
                //.....
                //populate all the attributes
             }
        });

First of all, I would use XDocument.Load instead of XElement.Load , because your XML is a document, with declaration, etc. 首先,我将使用XDocument.Load而不是XElement.Load ,因为您的XML是带有声明等的文档。

var xDoc = XDocument.Load("Input.txt");

Then, I'd set two local variables to avoid querying for the same thing more than once: 然后,我将设置两个局部变量以避免对同一事物进行多次查询:

var resp = xDoc.Root;
var status = resp.Element("status");

And use them to get what you need: 并使用它们来获取所需的内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM