如何使用 Xelement 创建 xml 文档
how to use create a xml document with Xelement
我正在尝试使用 Xelement 创建 xml 文档。我有一个包含 2 个对象的列表,数据和产品。每个都有一个对象列表。 xml 文档需要这样设置
<Property>
<Points>
<Sqft>1432</Sqft>
<Price>45666</Price>
<Product>Product Name</Product>
</Points>
</Property>
这是我正在使用的代码,当然它不起作用,但这是我理论上需要它做的事情。
var xEleProperty = new XElement("Property",
from item in Property.Data && item1 in Property.Products
select new XElement("points",
new XElement("Sqft", item.sqft),
new XElement("Price", item.price),
new XElement("Product", item1.product)
));
如果序列化适用,我会使用这个(未经测试 - 抱歉)
[XmlRoot("Property")]//you can change this if required
public class Property
{
Public List<Point> points{get;set;};
}
public class Point
{
public double Sqft{get;set;}
public double price{get;set;}
public string Product{get;set;}
}
public static void Main(string[] args)
{
Property p = new Property();
p.Sqft = 4;
p.Product= "Speaker";
p.Price = 120;
Serialize(p);
}
static public void Serialize(Property p)
{
XmlSerializer serializer = new XmlSerializer(typeof(p));
using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
{
serializer.Serialize(writer, p);
}
}
如果您的意思是您的文件没有被创建,请使用 Xelement
的 .Save
方法
下面的工作,你需要Id属性加入两个集合,否则你会得到重复的记录:
class Program
{
static void Main(string[] args)
{
var data = new List<Data>();
var products = new List<Product>();
data.Add(new Data { ProductId = 1, Price = 321.0, Sqft = 789 });
products.Add(new Product { Id = 1, Name = "SomeProduct 1" });
data.Add(new Data { ProductId = 2, Price = 123.0, Sqft = 456 });
products.Add(new Product { Id = 2, Name = "SomeProduct 2" });
var xEleProperty = new XElement("Property",
from d in data
join product in products on d.ProductId equals product.Id
select new XElement("Points",
new XElement("Sqft", d.Sqft),
new XElement("Price", d.Price),
new XElement("Product", product.Name)
));
Console.WriteLine(xEleProperty);
Console.ReadLine();
}
}
public class Data
{
public int ProductId { get; set; }
public double Price { get; set; }
public int Sqft { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
输出是:
<Property>
<Points>
<Sqft>789</Sqft>
<Price>321</Price>
<Product>SomeProduct 1</Product>
</Points>
<Points>
<Sqft>456</Sqft>
<Price>123</Price>
<Product>SomeProduct 2</Product>
</Points>
</Property>
我正在尝试使用 Xelement 创建 xml 文档。我有一个包含 2 个对象的列表,数据和产品。每个都有一个对象列表。 xml 文档需要这样设置
<Property>
<Points>
<Sqft>1432</Sqft>
<Price>45666</Price>
<Product>Product Name</Product>
</Points>
</Property>
这是我正在使用的代码,当然它不起作用,但这是我理论上需要它做的事情。
var xEleProperty = new XElement("Property",
from item in Property.Data && item1 in Property.Products
select new XElement("points",
new XElement("Sqft", item.sqft),
new XElement("Price", item.price),
new XElement("Product", item1.product)
));
如果序列化适用,我会使用这个(未经测试 - 抱歉)
[XmlRoot("Property")]//you can change this if required
public class Property
{
Public List<Point> points{get;set;};
}
public class Point
{
public double Sqft{get;set;}
public double price{get;set;}
public string Product{get;set;}
}
public static void Main(string[] args)
{
Property p = new Property();
p.Sqft = 4;
p.Product= "Speaker";
p.Price = 120;
Serialize(p);
}
static public void Serialize(Property p)
{
XmlSerializer serializer = new XmlSerializer(typeof(p));
using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
{
serializer.Serialize(writer, p);
}
}
如果您的意思是您的文件没有被创建,请使用 Xelement
的.Save
方法
下面的工作,你需要Id属性加入两个集合,否则你会得到重复的记录:
class Program
{
static void Main(string[] args)
{
var data = new List<Data>();
var products = new List<Product>();
data.Add(new Data { ProductId = 1, Price = 321.0, Sqft = 789 });
products.Add(new Product { Id = 1, Name = "SomeProduct 1" });
data.Add(new Data { ProductId = 2, Price = 123.0, Sqft = 456 });
products.Add(new Product { Id = 2, Name = "SomeProduct 2" });
var xEleProperty = new XElement("Property",
from d in data
join product in products on d.ProductId equals product.Id
select new XElement("Points",
new XElement("Sqft", d.Sqft),
new XElement("Price", d.Price),
new XElement("Product", product.Name)
));
Console.WriteLine(xEleProperty);
Console.ReadLine();
}
}
public class Data
{
public int ProductId { get; set; }
public double Price { get; set; }
public int Sqft { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
输出是:
<Property>
<Points>
<Sqft>789</Sqft>
<Price>321</Price>
<Product>SomeProduct 1</Product>
</Points>
<Points>
<Sqft>456</Sqft>
<Price>123</Price>
<Product>SomeProduct 2</Product>
</Points>
</Property>