使用Linq遍历XML文件到XML不能正常工作

时间:2019-06-10 23:43:37

标签: c# xml linq linq-to-xml

很抱歉有一些基本问题,但是我能说什么。我不知道。问题在于,存在一个foreach循环,该循环应该遍历行(各节),并且它在第一节中有效,而第二遍遍历该循环,则似乎没有读取第二节。相同的数据存储在 version 中。顺便说一句,该方法的调用方式将作为参数传递 ProductName (这里将表示多个产品,还需要过滤一个版本号(例如v2.0.0))结果也是如此。

所以我有一个看起来像这样的XML文件:

<Products>
  <ProductName1>
    <v2.0.0>
      <GUID>"{B5ECEC43-5406-4E4D-96D9-456823100313}"</GUID>
      <VersionNameToUninstall>"2.0.0 - 2.0.2"</VersionNameToUninstall>
      <UninstallResponseFile>"GVQC-Client-2.0.0-Uninst.iss"</UninstallResponseFile>
    </v2.0.0>
    <v2.0.3>
      <GUID>"{1D6C02D7-8E87-43BE-8AB2-1FF0E5ACD410}"</GUID>
      <VersionNameToUninstall>"2.0.3"</VersionNameToUninstall>
      <UninstallResponseFile>"GVQC-Client-2.0.3-Uninst.iss"</UninstallResponseFile>
    </v2.0.3>  
  </ProductName1>
  <ProductName2>
    <v3.0.0>
      <GUID>"{ABCDEC43-5406-4E4D-96D9-456823101234}"</GUID>
      <VersionNameToUninstall>"2.2.0 - 2.2.2"</VersionNameToUninstall>
      <UninstallResponseFile>"GVQC-Client-2.2.0-Uninst.iss"</UninstallResponseFile>
    </v3.0.0>
    <v4.0.0>
      <GUID>"{5D6C02D7-8E87-43BE-8AB2-1FF0E5ACD589}"</GUID>
      <VersionNameToUninstall>"4.0.0"</VersionNameToUninstall>
      <UninstallResponseFile>"GVQC-Client-4.0.0-Uninst.iss"</UninstallResponseFile>
    </v4.0.0>  
  </ProductName2>
</Products>

只有大约10个版本(例如v2.x.x),因此此处的数据并不多。因此,我创建了一个多维(嵌套)类/结构来保存数据,当我尝试用代码读取数据时,它就无法正常工作。

以下是我要填充的类/结构(我都尝试过但都没有用):

public class TopLevelObject
    {
        public string Version { get; set; }
        public RowLevelObject Row {get;set;}
    }
public struct RowLevelObject
    {
        public string Guid { get; set; }
        public string VersionName { get; set; }
        public string UninstallFileName { get; set; }
    }

这是我的代码。请忽略流-这样我就可以将此XML文件嵌入.exe中,而不必将其作为单独的文件:

public static List<TopLevelObject> GetGUIDSFromFile(string GUIDKey)

        List<InstallScriptMSIXMLTopLevelObject> installScriptMSIXMLTopLevelObjectList = new List<InstallScriptMSIXMLTopLevelObject>();

        Stream GUIDXmlFileStream = typeof(PGCommonCA).Assembly.GetManifestResourceStream("PGCommonCA.ProductGUIDs.xml");
        XElement xElement = XElement.Load(GUIDXmlFileStream);
        var versions = xElement.Elements(GUIDKey).Descendants();
        foreach (var version in versions)
        {
            TopLevelObject topLevelObject = new TopLevelObject();
            RowLevelObject rowLevelObject = new RowLevelObject();
            TopLevelObject.Version = version.Name.LocalName;

            RowLevelObject.Guid = version.Element("GUID").Value;
            RowLevelObject.VersionName = version.Element("VersionNameToUninstall").Value;
            RowLevelObject.UninstallFileName = version.Element("UninstallResponseFile").Value;
            TopLevelObjectList.Add(topLevelObject);
        }
        return TopLevelObjectList;
    }

我知道有很多读取XML的方法,但是我的选择不起作用,所以我正在寻找另一个简单解决方案。

2 个答案:

答案 0 :(得分:0)

以下作品:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement productName = doc.Root;

            List<TopLevelObject> top = productName.Elements().Select(x => new TopLevelObject() {
                Version = x.Name.LocalName,
                Row = new RowLevelObject() {
                    Guid = (string)x.Element("GUID"),
                    VersionName = (string)x.Element("VersionNameToUninstall"),
                    UninstallFileName = (string)x.Element("UninstallResponseFile")
                }
            }).ToList();

        }
    }
    public class TopLevelObject
    {
        public string Version { get; set; }
        public RowLevelObject Row { get; set; }
    }
    public struct RowLevelObject
    {
        public string Guid { get; set; }
        public string VersionName { get; set; }
        public string UninstallFileName { get; set; }
    }

}

答案 1 :(得分:0)

我知道了(非常感谢jdweng!)。这是基于顶部修改后的XML的最终解决方案:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
    const string FILENAME = @"c:\temp\test.xml";
    static TopLevelObject GetInfo(string xmlKey)
    {
        XDocument doc = XDocument.Load(FILENAME);
        XElement productName = doc.Root;

        List<TopLevelObject> top = productName.Descendants(xmlKey).Elements().Select(x => new TopLevelObject() {
            Version = x.Name.LocalName,
            Row = new RowLevelObject() {
                Guid = (string)x.Element("GUID"),
                VersionName = (string)x.Element("VersionNameToUninstall"),
                UninstallFileName = (string)x.Element("UninstallResponseFile")
            }
        }).ToList();

    }
}
public class TopLevelObject
{
    public string Version { get; set; }
    public RowLevelObject Row { get; set; }
}
public struct RowLevelObject
{
    public string Guid { get; set; }
    public string VersionName { get; set; }
    public string UninstallFileName { get; set; }
}

}