Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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# 从整个XDocument中提取属性_C#_Xml_Linq_Xml Parsing - Fatal编程技术网

C# 从整个XDocument中提取属性

C# 从整个XDocument中提取属性,c#,xml,linq,xml-parsing,C#,Xml,Linq,Xml Parsing,以下XDocument包含存储在XElement成员中的(x,y)坐标,这些坐标在属性“x”、“y”、“x1”、“y1”、“z”和“z1”下具有不同的名称: <?xml version="1.0" encoding="utf-8"?> <shapes> <shape> <connections /> <foreground> <strokewidth width="0.1" />

以下XDocument包含存储在XElement成员中的(x,y)坐标,这些坐标在属性“x”、“y”、“x1”、“y1”、“z”和“z1”下具有不同的名称:

<?xml version="1.0" encoding="utf-8"?>
<shapes>
  <shape>
    <connections />
    <foreground>
      <strokewidth width="0.1" />
      <path>
        <move x="1395.6" y="84.6" />
        <line x="80.1" y="84.6" />
        <curve x1="75.1" y1="84.6" x2="71.1" y2="88.6" x3="71.1" y3="93.6" />
        <line x="71.1" y="402.6" />
        <close />
      </path>
      <fillstroke />
    </foreground>
  </shape>
</shapes>
对于“y”、“y1”也是一样的


编辑: 由于坐标始终位于'leaf'xml元素中,因此我最终使用了:

return fileXml.Root.Descendants()
                .Where(e => e.HasElements == false) // leaf element
                .Where(e => e.Attribute("x") != null)
                .Select(c => c.Attribute("x").Value).Select(float.Parse)
                .ToList(); // or .ToArray();

它也可以包装在助手函数中。

您可以使用xpath。XPath表达式
/@x
将匹配名为
x
的所有属性:

var doc = XDocument.Parse(yourXml);
float[] x = ((IEnumerable)doc.XPathEvaluate("//@x"))
           .OfType<XAttribute>()
           .Select(c => float.Parse(c.Value, CultureInfo.InvariantCulture))
           .ToArray();
var doc=XDocument.Parse(yourXml);
float[]x=((IEnumerable)doc.xpatheevalue(“/@x”))
第()类
.Select(c=>float.Parse(c.Value,CultureInfo.InvariantCulture))
.ToArray();

有些人可能喜欢这个解决方案,有些人可能不喜欢。这有点复杂,但是使用XMLLINQ将整个xml解析为x,y对

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


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

            List<KeyValuePair<string, List<KeyValuePair<double, double>>>> instructions = new List<KeyValuePair<string, List<KeyValuePair<double, double>>>>();

            foreach (XElement instruction in doc.Descendants("path").FirstOrDefault().Elements())
            {
                List<KeyValuePair<double, double>> points = new List<KeyValuePair<double, double>>();
                for (int i = 0; i < instruction.Attributes().Count(); i += 2)
                {
                    points.Add(new KeyValuePair<double,double>((double)instruction.Attributes().Skip(i).FirstOrDefault(), (double)instruction.Attributes().Skip(i + 1).FirstOrDefault()));
                }
                instructions.Add(new KeyValuePair<string, List<KeyValuePair<double,double>>>( instruction.Name.LocalName, points));
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
列表说明=新列表();
foreach(doc.substands(“path”).FirstOrDefault().Elements()中的XElement指令)
{
列表点=新列表();
对于(int i=0;i
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


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

            List<KeyValuePair<string, List<KeyValuePair<double, double>>>> instructions = new List<KeyValuePair<string, List<KeyValuePair<double, double>>>>();

            foreach (XElement instruction in doc.Descendants("path").FirstOrDefault().Elements())
            {
                List<KeyValuePair<double, double>> points = new List<KeyValuePair<double, double>>();
                for (int i = 0; i < instruction.Attributes().Count(); i += 2)
                {
                    points.Add(new KeyValuePair<double,double>((double)instruction.Attributes().Skip(i).FirstOrDefault(), (double)instruction.Attributes().Skip(i + 1).FirstOrDefault()));
                }
                instructions.Add(new KeyValuePair<string, List<KeyValuePair<double,double>>>( instruction.Name.LocalName, points));
            }

        }
    }
}