C# C使用linq反序列化xml
我有以下xml文件C# C使用linq反序列化xml,c#,xml,linq,xml-deserialization,C#,Xml,Linq,Xml Deserialization,我有以下xml文件 <?xml version="1.0" encoding="utf-8"?> <Launchpad> <Shortcuts> <Shortcut Id="1"> <Type>Folder</Type> <FullPath>C:\bla\bla\bla</FullPath> <Name>Proximity</Nam
<?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.AttributeId为null
如果有人对改进linq查询或为什么属性不起作用有任何建议,这将是一个很大的帮助您必须阅读.genderantsshortcut而不是.genderantsshortcuts。 大概是这样的:
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();
您必须阅读.genderantsshortcut而不是.genderantsshortcuts。 大概是这样的:
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();
我通过选择快捷方式作为快捷方式的后代来获得完整列表 此外,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();
我通过选择快捷方式作为快捷方式的后代来获得完整列表 此外,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级别有点困惑。谢谢Alex和Tom!当我改为快捷方式时,我的代码起作用了。我对操作的xml级别有点困惑。