var ships = (from sh in player.Elements("ship")
select new am.Ship
{ id= Convert.ToInt16(sh.Attribute("id").Value),
type = sh.Attribute("type").Value,
hpWidth = 15,
coord = 25* Convert.ToInt16(sh.Attribute("cox").Value) + Convert.ToInt16(sh.Attribute("coy").Value),
direction = Convert.ToInt16(sh.Attribute("dir").Value),
}).ToList();
This query returns via ajax an xml string with each attribute as a new xml node:
<ship> <id>123</id> <type>cdf</type> ... </ship>
and so on.
How would I write a query to return the data as
<ship id="123" type="cdf"...></ship>
Also, is it always valid to autoclose the tag? Meaning I would have
<ship id="123" type="cdf"... />
EDIT:
I guess one way would be to make a list of strings that would be formated as these elements?
can you give me just 1 line of code of how to do that? I used
public class Ship
{
public Ship()
{
}
[XmlAttribute("id")]
public int id { get; set; }
public string type { get; set; }
public int hpWidth { get; set; }
public int coord { get; set; }
public int direction { get; set; }
}
and got an error
'System.Xml.XmlAttribute' is not an attribute class
The XmlAttribute is belong to “System.Xml.Serialization” namespace, please try refer to the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
namespace LinqToXML
{
public class Ship
{
public Ship()
{
}
[XmlAttribute("id")]
public int id { get; set; }
public string type { get; set; }
public int hpWidth { get; set; }
public int coord { get; set; }
public int direction { get; set; }
}
}
Member
24 Points
51 Posts
linq to xml - return xml with atributes
Jul 31, 2013 05:25 PM|cdf53|LINK
This query returns via ajax an xml string with each attribute as a new xml node:
<ship> <id>123</id> <type>cdf</type> ... </ship>
and so on.
How would I write a query to return the data as
<ship id="123" type="cdf"...></ship>
Also, is it always valid to autoclose the tag? Meaning I would have
<ship id="123" type="cdf"... />
EDIT:
I guess one way would be to make a list of strings that would be formated as these elements?
Contributor
6607 Points
1822 Posts
Re: linq to xml - return xml with atributes
Aug 01, 2013 01:26 AM|fayaz_3e|LINK
The better way is to mark your Ship class properties as attribues. This should auto generate the attribute diven XML.
Add the [XmlAttribute] attribute on all properties of Ship class...
Member
24 Points
51 Posts
Re: linq to xml - return xml with atributes
Aug 01, 2013 10:10 AM|cdf53|LINK
can you give me just 1 line of code of how to do that? I used
and got an error
'System.Xml.XmlAttribute' is not an attribute class
Star
12777 Points
1635 Posts
Re: linq to xml - return xml with atributes
Aug 07, 2013 02:05 AM|Terry Guo - MSFT|LINK
Hi cdf53
The XmlAttribute is belong to “System.Xml.Serialization” namespace, please try refer to the following code.
Best Regards,
Terry Guo