Как прочитать xml значения атрибутов в c# списке - PullRequest
0 голосов
/ 15 апреля 2020

У меня есть xml данные с кодом ниже

    <RESULT>
<OperationsRow>
    <Name>John</InteNom>
    <Id>XYV10572</Id>
    <OperationDate>2020-04-14T13:25:41</OperationDate>
    <OperationDetails>welcome to the world</OperationDetails>
    <OperationDesc>ABCDEF</OperationDesc>
    </OperationsRow>

<OperationsRow>
    <Name>Kennedy</InteNom>
    <Id>XYM54572</Id>
    <OperationDate>2018-04-14T13:11:41</OperationDate>
    <OperationDetails>welcome to Newyork/OperationDetails>
    <OperationDesc>ZYX</OperationDesc>
    </OperationsRow>  
</RESULT>

Как прочитать значения атрибута xml в список операций (Список) с помощью c# linq

Пожалуйста, помогите мне здесь.

Спасибо

1 Ответ

1 голос
/ 15 апреля 2020

Вот ваш класс операций

 public class Operations
{
    public string Name { get; set; }
    public string Id { get; set; }
    public string OperationDate { get; set; }
    public string OperationDetails { get; set; }
    public string OperationDesc { get; set; }
}

Теперь нам нужно List<Operations> из XML.

Вот полный код с реализацией LINQ

namespace ConsoleApplication
{

class Program
    {

        public static string xml = @"
    <result>
    <OperationsRow>
        <Name>John</Name>
        <Id>XYV10572</Id>
        <OperationDate>2020-04-14T13:25:41</OperationDate>
        <OperationDetails>welcome to the world</OperationDetails>
        <OperationDesc>ABCDEF</OperationDesc>
     </OperationsRow>

    <OperationsRow>
        <Name>Kennedy</Name>
        <Id>XYM54572</Id>
        <OperationDate>2018-04-14T13:11:41</OperationDate>
        <OperationDetails>welcome to Newyork</OperationDetails>
        <OperationDesc>ZYX</OperationDesc>
    </OperationsRow>  
    </result>";

        static void Main(string[] args)
        {
            XDocument doc = XDocument.Parse(xml);

            var operations =
                from operation in doc.Descendants("result").Elements("OperationsRow")
                select new Operations() 
                { 
                    Id = operation.Element("Id").Value,
                    Name = operation.Element("Name").Value,
                    OperationDate = operation.Element("OperationDate").Value,
                    OperationDetails = operation.Element("OperationDetails").Value,
                    OperationDesc = operation.Element("OperationDesc").Value
                };
        }
}

Вы можете отслеживать структуру:

from operations in xml
      .Element("result")       // must be root
      .Elements("OperationsRow")         // only directly children of CompanyInfo

или менее строго

from employee in xml.Descendants("OperationsRow")    // all employees at any level

И затем получить необходимую информацию:

   select new Operations()
   {
       Id = operation.Element("Id").Value,
       Name = operation.Element("Name").Value
        //........
   }

Надеюсь, это поможет ,

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...