Xml 将元素转换为数组
我应该如何将如下所示的XElement转换为点数组(点可以是一个包含变量X和Y的类):Xml 将元素转换为数组,xml,linq-to-sql,xelement,Xml,Linq To Sql,Xelement,我应该如何将如下所示的XElement转换为点数组(点可以是一个包含变量X和Y的类): 这对我来说很有效,我能够从xe XElement获取一个数组。(不过,还有更好的办法) 这对我来说很有效,我能够从xe XElement获得一个数组。(不过,还有更好的办法) 您只需将castXAttribute键入int: Point[]points=(来自xe.Elements中的pt(“点”) 设x=(int)pt.Attribute(“x”) 设y=(int)pt.Attribute(“y”) 选择新
这对我来说很有效,我能够从xe XElement获取一个数组。(不过,还有更好的办法)
这对我来说很有效,我能够从xe XElement获得一个数组。(不过,还有更好的办法)
您只需将cast
XAttribute
键入int
:
Point[]points=(来自xe.Elements中的pt(“点”)
设x=(int)pt.Attribute(“x”)
设y=(int)pt.Attribute(“y”)
选择新点(x,y)).ToArray();
这样,如果在当前元素中找不到属性,就不会抛出异常,更不用说它更短了。或者,如果您更喜欢方法语法:
Point[]points=xe.Elements(“点”)
.选择(p=>新点((int)p.Attribute(“X”),(int)p.Attribute(“Y”))
.ToArray();
您只需将castXAttribute
键入int
:
Point[]points=(来自xe.Elements中的pt(“点”)
设x=(int)pt.Attribute(“x”)
设y=(int)pt.Attribute(“y”)
选择新点(x,y)).ToArray();
这样,如果在当前元素中找不到属性,就不会抛出异常,更不用说它更短了。或者,如果您更喜欢方法语法:
Point[]points=xe.Elements(“点”)
.选择(p=>新点((int)p.Attribute(“X”),(int)p.Attribute(“Y”))
.ToArray();
您使用哪种操作系统?有哪些工具可用?@cyrus iam在windows上使用.net 4.5,vs2012@Anonymous什么语言C#/VB?到目前为止,您尝试过什么吗?您使用的是哪种操作系统?有哪些工具可用?@cyrus iam在windows上使用.net 4.5,vs2012@Anonymous什么语言C#/VB?到目前为止你试过什么吗?
<Points xmlns="">
<Point X="420" Y="240" />
<Point X="400" Y="298" />
<Point X="350" Y="335" />
<Point X="289" Y="335" />
<Point X="239" Y="298" />
<Point X="220" Y="239" />
<Point X="239" Y="181" />
<Point X="289" Y="144" />
<Point X="350" Y="144" />
<Point X="400" Y="181" />
</Points>
Point[] points = (from pt in xe.Elements("Point")
let x = Convert.ToInt32(pt.Attribute("X").Value)
let y = Convert.ToInt32(pt.Attribute("Y").Value)
select new Point(x, y)).ToArray();