C# 如何将xml文件数据存储到C中的列表中,并使用LINQ在控制台上显示?
我必须在模块名的基础上使用LING将xml文件的数据存储到c中的列表中,并在控制台上显示该数据。下面是我的xml文件格式。我尝试了很多方法,但都没有成功。我是C的新手C# 如何将xml文件数据存储到C中的列表中,并使用LINQ在控制台上显示?,c#,C#,我必须在模块名的基础上使用LING将xml文件的数据存储到c中的列表中,并在控制台上显示该数据。下面是我的xml文件格式。我尝试了很多方法,但都没有成功。我是C的新手 <getMetadata> <Module Name="Doors_Module2"> <Field> <AttributeName>TableType</AttributeName> <AttributeType>TableType</A
<getMetadata>
<Module Name="Doors_Module2">
<Field>
<AttributeName>TableType</AttributeName>
<AttributeType>TableType</AttributeType>
</Field>
<Field>
<AttributeName>TableTopBorder</AttributeName>
<AttributeType>TableEdgeType</AttributeType>
</Field>
<Field>
<AttributeName>TableShowWide</AttributeName>
<AttributeType>Boolean</AttributeType>
</Field>
<Field>
<AttributeName>TableShowBookform</AttributeName>
<AttributeType>Boolean</AttributeType>
</Field>
<Field>
<AttributeName>TableShowAttrs</AttributeName>
<AttributeType>Boolean</AttributeType>
</Field>
<Field>
<AttributeName>TableRightBorder</AttributeName>
<AttributeType>TableEdgeType</AttributeType>
</Field>
<Field>
<AttributeName>TableLinkIndicators</AttributeName>
<AttributeType>Boolean</AttributeType>
</Field>
</Module>
<Module Name="Doors_Module1">
<Field>
<AttributeName>TableType</AttributeName>
<AttributeType>TableType</AttributeType>
</Field>
<Field>
<AttributeName>TableTopBorder</AttributeName>
<AttributeType>TableEdgeType</AttributeType>
</Field>
<Field>
<AttributeName>TableShowWide</AttributeName>
<AttributeType>Boolean</AttributeType>
</Field>
</Module>
</getMetadata>
这里有一个建议:
public class Module
{
public string Name { get; set; }
public List<Field> Fields { get; set; }
}
public class Field
{
public string AttributeName { get; set; }
public string AttributeType { get; set; }
}
XDocument loXDocument = XDocument.Parse(lsXmlString);
var loList = (from module in loXDocument.Descendants("Module")
let fields = module.Descendants("Field").Select(item => new Field
{
AttributeName = item.Element("AttributeName").Value,
AttributeType = item.Element("AttributeType").Value,
})
select new Module
{
Name = module.Attribute("Name").Value,
Fields = fields.ToList()
}).ToList();
foreach (var loModule in loList)
{
Console.WriteLine($"Module: {loModule.Name}");
foreach (var loField in loModule.Fields)
Console.WriteLine($"AttributeName: {loField.AttributeName}, AttributeType{loField.AttributeType}");
}
为什么要在列表中存储xml文件?您的目的是将xml文件输出到控制台?那你就不需要清单了。闻起来像是家庭作业。我尝试了很多选择-你尝试了什么?在向社区寻求帮助之前,至少要表明你自己已经付出了一些努力。您编写的哪些代码未能做到这一点?您可能能够读入数据集。我不知道这在您的案例中效果如何,因为您只发布了一段xml。Try:DataSet ds=新数据集;ds.ReadXmlFILENAME;