[英]Deserialize XML into a C# object
How can I desearlize the below CatalogProduct tags into my CatalogProduct object using C#? 如何使用C#将以下CatalogProduct标记解除扩展到我的CatalogProduct对象?
<?xml version="1.0" encoding="UTF-8"?>
<CatalogProducts>
<CatalogProduct Name="MyName1" Version="1.1.0"/>
<CatalogProduct Name="MyName2" Version="1.1.0"/>
</CatalogProducts>
Note i don't have a CatalogProducts object so want to skip that element when pulling back the into to deserialize 注意我没有CatalogProducts对象,所以想要在拉回到反序列化时跳过该元素
Thanks 谢谢
var xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<CatalogProducts>" +
"<CatalogProduct Name=\"MyName1\" Version=\"1.1.0\"/>" +
"<CatalogProduct Name=\"MyName2\" Version=\"1.1.0\"/>" +
"</CatalogProducts>";
var document = XDocument.Parse(xml);
IEnumerable<CatalogProduct> catalogProducts =
from c in productsXml.Descendants("CatalogProduct")
select new CatalogProduct
{
Name = c.Attribute("Name").Value,
Version = c.Attribute("Version").Value
};
Just for your information, here's an example how to really serialize and deserialize an object: 仅供参考,以下是如何真正序列化和反序列化对象的示例:
private CatalogProduct Load()
{
var serializer = new XmlSerializer(typeof(CatalogProduct));
using (var xmlReader = new XmlTextReader("CatalogProduct.xml"))
{
if (serializer.CanDeserialize(xmlReader))
{
return serializer.Deserialize(xmlReader) as CatalogProduct;
}
}
}
private void Save(CatalogProduct cp)
{
using (var fileStream = new FileStream("CatalogProduct.xml", FileMode.Create))
{
var serializer = new XmlSerializer(typeof(CatalogProduct));
serializer.Serialize(fileStream, cp);
}
}
The canonical method would be to use the xsd.exe
tool twice. 规范方法是两次使用
xsd.exe
工具。 First, to create a schema from your example XML as so: 首先,从示例XML创建一个模式,如下所示:
xsd.exe file.xml
will generate file.xsd. xsd.exe file.xml
将生成file.xsd。
Then: 然后:
xsd.exe /c file.xsd
will generate file.cs. xsd.exe /c file.xsd
将生成file.cs.
File.cs will be the object(s) you can deserialize your XML from using any one of the techniques that you can easily find here, eg this . File.cs将成为对象()你可以使用的,你可以很容易地发现这里的技术中的任何一个,例如反序列化的XML 这个 。
Without "CatalogProduct" object i think it's very difficult, maybe with the dynamic type of .net 4.0 it's possible, but i'm not sure. 如果没有“CatalogProduct”对象,我认为这很难,也许是动态类型的.net 4.0可能,但我不确定。
The only way i know, is to utilize the XmlSerializer class with Deserialize method, but you need the object CatalogProduct. 我知道的唯一方法是使用带有Deserialize方法的XmlSerializer类,但是你需要对象CatalogProduct。
I hope the following link is useful: http://blogs.msdn.com/b/alexghi/archive/2008/12/22/using-anonymous-types-to-deserialize-json-data.aspx 我希望以下链接很有用: http : //blogs.msdn.com/b/alexghi/archive/2008/12/22/using-anonymous-types-to-deserialize-json-data.aspx
Assuming your CatalogProduct
object looks something like this: 假设您的
CatalogProduct
对象看起来像这样:
public class CatalogProduct {
public string Name;
public string Version;
}
I think Linq to Xml will be the simplest and fastest way for you 我认为Linq to Xml将是最简单,最快捷的方式
var cps1 = new[] { new CatalogProduct { Name = "Name 1", Version = "Version 1" },
new CatalogProduct { Name = "Name 2", Version = "Version 2" } };
var xml = new XElement("CatalogProducts",
from c in cps1
select new XElement("CatalogProduct",
new XAttribute("Name", c.Name),
new XAttribute("Version", c.Version)));
// Use the following to deserialize you objects
var cps2 = xml.Elements("CatalogProduct").Select(x =>
new CatalogProduct {
Name = (string)x.Attribute("Name"),
Version = (string)x.Attribute("Version") }).ToArray();
Please note that .NET offers true object graph serialization which I have not shown 请注意,.NET提供了我尚未显示的真实对象图序列化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.