C# C将XML解析为不同的列表

C# C将XML解析为不同的列表,c#,xml,parsing,xamarin.forms,C#,Xml,Parsing,Xamarin.forms,我有个问题。 在我的Xamarin表单应用程序中,我对我的网页进行网络调用,在那里我收集XML以在应用程序中使用。我将XML解析为3个不同的列表 相册 图像 格式 下面是我的xml的样子: <Data> <Albums> <Album> <Image></Image> <Image></Image> <Image

我有个问题。 在我的Xamarin表单应用程序中,我对我的网页进行网络调用,在那里我收集XML以在应用程序中使用。我将XML解析为3个不同的列表

相册 图像 格式 下面是我的xml的样子:

<Data>
    <Albums>
        <Album>
            <Image></Image>
            <Image></Image>
            <Image></Image>
        </Album>
        <Album>
            <Image></Image>
            <Image></Image>
        </Album>
    </Albums>
    <Images>
        <Image></Image>
        <Image></Image>
        <Image></Image>
        <Image></Image>
    </Images>
    <Formats>
        <Format></Format>
        <Format></Format>
    </Formats>
</Data>
但很明显,它统计了完整xml中的所有标记,因此lstImages的计数为9,因为相册中有5个,相册中有4个

这是我的c代码:

if (!string.IsNullOrEmpty(xmlString))
{
    doc = XDocument.Parse(xmlString);
}

//Check if xml has any elements 
if (!string.IsNullOrEmpty(xmlString) && doc.Root.Elements().Any())
{
    App.lstAlbums = doc.Descendants("Albums").Descendants("Album").Select(d =>
    new Album
    {
        Id = Convert.ToInt32(d.Element("Id").Value),
        Name = d.Element("Name").Value,
        Images = doc.Descendants("Album").Descendants("Image").Select(a =>
            new myImage
            {
                Id = Convert.ToInt32(a.Element("Id").Value),
                Name = a.Element("Name").Value,
                Size = a.Element("Size").Value,
                Price = Convert.ToDecimal(a.Element("Price").Value)
            }).ToList(),
        Prijs = Convert.ToDecimal(d.Element("Price").Value)
    }).ToList();

    App.lstImages = doc.Descendants("Images").Descendants("Image").Select(e =>
    new myImage
    {
        Id = Convert.ToInt32(e.Element("Id").Value),
        Name = e.Element("Name").Value
    }).ToList();

    App.lstFormats = doc.Descendants("Formats").Descendants("Format").Select(e =>
    new Format
    {
        Id = Convert.ToInt32(e.Element("Id").Value),
        Size = e.Element("Size").Value,
        Price = Convert.ToDecimal(e.Element("Price").Value)
    }).ToList();
}
如何修复此问题?

尝试使用xPath

差不多

(IEnumerable)doc.XPathEvaluate("//Albums/Album")


坦率地说,这是XmlSerializer的工作。以下方面应起作用:

[XmlRootData] 公共类MyData{ [XmlArrayAlbums] [XmlArrayItemAlbum]
公共列表,但请注意,这在数组等方面做得并不好,派生方法是递归的;如果您只想查看一个级别,请改用元素,但坦率地说,这似乎非常适合XmlSerializer,而不是手动解析。您能为我的一个列表(例如相册)提供一个示例代码吗在此情况下,手动解析只是已经存在的大量代码。我如何将其分配到列表中?迭代IEnumerable或从中选择。您需要将IEnumerable中的每个项强制转换为XElement,因为您知道XML结构是什么。然后从XElement中提取属性。XElement具有属性和Attributes方法。XmlSerializer可能是一个更好的选择,但这对于您已经阅读的代码来说应该是一个简单的交换。好的,我添加了您的代码,但它在这一行崩溃:var serializer=new XmlSerializertypeofMyData;错误为:System.InvalidoOperationException:“出现了一个反映类型“MyApp.Models.MyData”的错误。@a.Vreeswij那么内部异常是怎么说的呢?它通常会非常喋喋不休地谈论问题所在is@A.Vreeswijk另外:在这里工作很好-您是否更改了可访问性?我想我为什么会出现此错误。我做了一些更改,因为您的代码不完整。我需要添加类:图像和格式。您已经添加了相册。可以吗请使用以下类更新代码,这样我就可以看到我做错了什么:Album=>Id,Name,List Images Image=>Id,Name,Price Format=>Id,Size@A.Vreeswijk这样地?
(IEnumerable)doc.XPathEvaluate("//Albums/Album")
(IEnumerable)doc.XPathEvaluate("//Images/Image")