Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Linq to XML中将XML属性转换为字典_C#_Linq To Xml - Fatal编程技术网

C# 在Linq to XML中将XML属性转换为字典

C# 在Linq to XML中将XML属性转换为字典,c#,linq-to-xml,C#,Linq To Xml,我有一个程序,需要将特定标记的两个属性转换为字典的键和值。XML如下所示: XNamespace ns = "http://the_namespace"; var startingpoints = from sp in xml.Elements(ns+"startingPoint") from el in sp.Attributes() select el.Value; Console.WriteLine(string.For

我有一个程序,需要将特定标记的两个属性转换为
字典
的键和值。XML如下所示:

XNamespace ns = "http://the_namespace";
var startingpoints = from sp in xml.Elements(ns+"startingPoint")
                 from el in sp.Attributes()
                 select el.Value;
Console.WriteLine(string.Format("Player 1: X = {0}, Y = {1}", 
                                startingpoints[1].X, 
                                startingpoints[1].Y));
(片段)


这让我得到了一个很好的
IEnumerable
,里面充满了像“1,1”和“1”这样的东西,但是应该有一种方法来调整像to do属性之类的东西,而不是元素。请帮点忙好吗?谢谢大家!

我假设您希望在字典中存储所有玩家及其各自起点坐标的映射。此操作的代码如下所示:

Dictionary<int, string> startingpoints = xml.Elements(ns + "startingPoint")
        .Select(sp => new { 
                              Player = (int)(sp.Attribute("player")), 
                              Coordinates = (string)(sp.Attribute("coordinates")) 
                          })
        .ToDictionary(sp => sp.Player, sp => sp.Coordinates);
然后可以立即将字符串解析为坐标:

Dictionary<int, string> startingpoints = xml.Elements(ns + "startingPoint")
        .Select(sp => new { 
                              Player = (int)(sp.Attribute("player")), 
                              Coordinates = Coordinate.FromString((string)(sp.Attribute("coordinates"))) 
                          })
        .ToDictionary(sp => sp.Player, sp => sp.Coordinates);

我想你想做这样的事情

string xml = @"<root>
                <startingPoint coordinates=""1,1"" player=""1"" />
                <startingPoint coordinates=""2,2"" player=""2"" />
               </root>";

XDocument document = XDocument.Parse(xml);

var query = (from startingPoint in document.Descendants("startingPoint")
             select new
             {
                 Key = (int)startingPoint.Attribute("player"),
                 Value = (string)startingPoint.Attribute("coordinates")
             }).ToDictionary(pair => pair.Key, pair => pair.Value);

foreach (KeyValuePair<int, string> pair in query)
{
    Console.WriteLine("{0}\t{1}", pair.Key, pair.Value);
}
stringxml=@”
";
XDocument document=XDocument.Parse(xml);
var query=(从document.subjects中的startingPoint(“startingPoint”)开始)
选择新的
{
Key=(int)startingPoint.Attribute(“player”),
Value=(字符串)startingPoint.Attribute(“坐标”)
}).ToDictionary(pair=>pair.Key,pair=>pair.Value);
foreach(查询中的KeyValuePair对)
{
WriteLine(“{0}\t{1}”,pair.Key,pair.Value);
}

生成的字典应该是什么样子?玩家作为键,他们的坐标作为值?是的,没错。这将通过一个getter传递,调用函数可以根据需要对其进行解析。
Console.WriteLine(string.Format("Player 1: X = {0}, Y = {1}", 
                                startingpoints[1].X, 
                                startingpoints[1].Y));
string xml = @"<root>
                <startingPoint coordinates=""1,1"" player=""1"" />
                <startingPoint coordinates=""2,2"" player=""2"" />
               </root>";

XDocument document = XDocument.Parse(xml);

var query = (from startingPoint in document.Descendants("startingPoint")
             select new
             {
                 Key = (int)startingPoint.Attribute("player"),
                 Value = (string)startingPoint.Attribute("coordinates")
             }).ToDictionary(pair => pair.Key, pair => pair.Value);

foreach (KeyValuePair<int, string> pair in query)
{
    Console.WriteLine("{0}\t{1}", pair.Key, pair.Value);
}