XML Descendants总是返回Empty结果

时间:2018-04-09 21:03:09

标签: c# xml linq-to-xml

我不知道为什么我无论如何都无法完成这项工作。

我的代码:

using System;
using System.Linq;
using System.Xml.Linq;

namespace XML_TESTER_CM
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            XDocument xDoc;
            xDoc = XDocument.Load("Palette_Sets.xml");

            var result1 = xDoc.Descendants("SETS");
            var result2 = xDoc.Descendants("PALETTE");
            var result3 = xDoc.Descendants("COLOR");
        }
    }
}

我的XML文件: https://pastecode.xyz/view/25d53914

<?xml version="1.0" encoding="utf-8"?> 

<SETS>
  <PALETTE id="Default Set">
    <COLOR>
      <Name>Red</Name>
      <RGBString>#FF0000</RGBString>
    </COLOR>
    <COLOR>
      <Name>Blue</Name>
      <RGBString>#0000FF</RGBString>
    </COLOR>
    <COLOR>
      <Name>Cyan</Name>
      <RGBString>#00FFFF</RGBString>
    </COLOR>
    <COLOR>
      <Name>Magenta</Name>
      <RGBString>#FF00FF</RGBString>
    </COLOR>
    <COLOR>
      <Name>Green</Name>
      <RGBString>#00FF00</RGBString>
    </COLOR>
    <COLOR>
      <Name>Orange</Name>
      <RGBString>#FFAA00</RGBString>
    </COLOR>
    <COLOR>
      <Name>Yellow</Name>
      <RGBString>#FFFF00</RGBString>
    </COLOR>
  </PALETTE>
  <PALETTE id="OLD Set">
    <COLOR>
      <Name>Olive</Name>
      <RGBString>#7f8229</RGBString>
    </COLOR>
    <COLOR>
      <Name>Dark Blue</Name>
      <RGBString>#272c72</RGBString>
    </COLOR>
    <COLOR>
      <Name>Silver</Name>
      <RGBString>#9597af</RGBString>
    </COLOR>
  </PALETTE>
</SETS>

xDoc正确返回文档,但是Descendants方法并不适用于每个resultX var都有一个空的结果视图。

1 个答案:

答案 0 :(得分:1)

我喜欢在xml中使用字典。下面我将您的xml转换为颜色:

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

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


            Dictionary<string, Dictionary<string, PaletteColor>> dict = doc.Descendants("PALETTE")
                .GroupBy(x => (string)x.Attribute("id"), y => y.Elements("COLOR").Select(c => new PaletteColor() { name = (string)c.Element("Name"), color = Color.FromArgb(int.Parse(((string)c.Element("RGBString")).Substring(1), System.Globalization.NumberStyles.HexNumber)) })
                    .GroupBy(a => a.name, b => b)
                    .ToDictionary(a => a.Key, b => b.FirstOrDefault()))
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
    public class PaletteColor
    {
        public string name { get; set; }
        public Color color { get; set; }
    }
}

这是使用list而不是2nd dictionary

的答案
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Drawing;

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


            Dictionary<string, List<PaletteColor>> dict = doc.Descendants("PALETTE")
                .GroupBy(x => (string)x.Attribute("id"), y => y.Elements("COLOR").Select(c => new PaletteColor() { name = (string)c.Element("Name"), color = Color.FromArgb(int.Parse(((string)c.Element("RGBString")).Substring(1), System.Globalization.NumberStyles.HexNumber)) }).ToList())
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
    public class PaletteColor
    {
        public string name { get; set; }
        public Color color { get; set; }
    }
}