XML 后代总是 return 空结果
XML Descendants always return Empty result
我不知道为什么无论如何我都做不到。
我的代码:
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 returns 文档正确,但 Descendants 方法根本不起作用每个 resultX var 都有一个空的结果视图。
我喜欢 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; }
}
}
这里是使用列表而不是第二个字典的答案
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; }
}
}
我不知道为什么无论如何我都做不到。
我的代码:
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 returns 文档正确,但 Descendants 方法根本不起作用每个 resultX var 都有一个空的结果视图。
我喜欢 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; }
}
}
这里是使用列表而不是第二个字典的答案
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; }
}
}