【问题标题】:C# deserialize xml using linqC# 使用 linq 反序列化 xml
【发布时间】:2017-07-05 23:41:06
【问题描述】:

我有以下 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") 为空。

如果有人对改进 linq 查询或属性不起作用的原因有任何建议,那将是一个很大的帮助

【问题讨论】:

    标签: c# xml linq xml-deserialization


    【解决方案1】:

    您必须阅读 .Descendants("Shortcut") 而不是 .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();
    

    【讨论】:

      【解决方案2】:

      我通过选择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();
      

      【讨论】:

      • 感谢亚历克斯和汤姆!当我更改为快捷方式时,我的代码有效。我对我正在操作的 xml 的“级别”有点困惑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2011-05-12
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      相关资源
      最近更新 更多