簡體   English   中英

C#使用Linq反序列化xml

[英]C# deserialize xml using linq

我有以下xml文件

<?xml version="1.0" encoding="utf-8"?>
<Launchpad>
  <Shortcuts>
    <Shortcut Id="1">
      <Type>Folder</Type>
       <FullPath>C:\bla\bla\bla</FullPath>
       <Name>Proximity</Name>
    </Shortcut>
    <Shortcut Id="2">
      <Type>Folder</Type>
      <FullPath>C:\bla</FullPath>
      <Name>Visual Studio 2017</Name>
    </Shortcut>
  </Shortcuts>
</Launchpad>

我正在嘗試反序列化為這樣的對象:(首先嘗試使用查詢語法,但也不起作用)

XDocument xd = XDocument.Load(FullPath);

// query syntax
//var shortcuts = (from s in xd.Descendants("Shortcuts")
//                 select new Shortcut()
//                 {
//                   Id = Convert.ToInt32(s.Attribute("Id")),
//                   TypeOfLink = GetTypeFromString(s.Descendants("Type") 
//                                .First()
//                                .Value),
//                   FullPathToTarget = s.Descendants("FullPath") 
//                                         .First()
//                                         .Value,
//                   Name = s.Descendants("Name").First().Value,
//                 }).ToList();

// method syntax
List<Shortcut> shortcuts = xd.Descendants("Shortcuts")
                             .Select(s => 
               new Shortcut()
               {
                 //Id = Convert.ToInt32(s.Attribute("Id")),
                 TypeOfLink = GetTypeFromString(s.Descendants("Type") 
                                                 .First().Value),
                 FullPathToTarget = s.Descendants("FullPath")
                                                 .First().Value,
                 Name = s.Descendants("Name")
                         .First().Value,
               }).ToList();
return shortcuts;

由於某種原因,我在列表中僅得到一個快捷方式對象。 同樣,由於某種原因,s.Attribute(“ Id”)為null。

如果有人對linq查詢有任何建議,或者為什么該屬性不起作用,那將是一個很大的幫助

您必須閱讀.Descendants(“ Shortcuts”)而不是.Descendants(“ Shortcuts”)。 像這樣:

List<Shortcut> shortcuts = xd.Descendants("Shortcut").Select(s =>
                       new Shortcut()
                       {
                           Id = s.Attribute("Id").Value, 
                           TypeOfLink = s.Descendants("Type").First().Value,
                           FullPathToTarget = s.Descendants("FullPath").First().Value,
                           Name = s.Descendants("Name").First().Value,
                       }).ToList();

我通過選擇獲取完整列表Shortcut的后裔Shortcuts

另外, ID是XAttribute,因此您不能將其轉換為int。

您需要使用Value來獲取屬性值。

XDocument xd = XDocument.Load(ms);
XElement root = xd.Document.Root;
var list = root.Descendants("Shortcuts").Descendants("Shortcut").Select(x =>
    new Shortcut()
    {
        Id = Convert.ToInt32(x.Attribute("Id").Value),
        TypeOfLink = GetTypeFromString(x.Descendants("Type")
                                        .First().Value),
        FullPathToTarget = x.Descendants("FullPath")
                                        .First().Value,
        Name = x.Descendants("Name").First().Value
        }).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM