为最简单的 xml 文件编写最简单的 XML 反序列化类。如何避免嵌套?在根目录反序列化?
我想反序列化一个 xml 文件,该文件必须采用这种形式
<Basket>
<Fruit>Apple</Fruit>
<Fruit>Orange</Fruit>
<Fruit>Grapes</Fruit>
</Basket>
。在我在互联网上阅读的示例中,我能找到的最不可能的格式如下,
<Basket>
<FruitArray>
<Fruit>Apple</Fruit>
</FruitArray>
<FruitArray>
<Fruit>Orange</Fruit>
</FruitArray>
<FruitArray>
<Fruit>Grapes</Fruit>
</FruitArray>
</Basket>
并且具有以下反序列化类,用于将其转换为类对象。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XMLSerialization_Basket
{
[System.Xml.Serialization.XmlRootAttribute("Basket", Namespace = "BasketNamespace", IsNullable = false)]
public class Basket
{
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FruitArray")]
public FruitArray[] objFruitArray;
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "BasketNamespace")]
public class FruitArray
{
/// <remarks/>
private string _Fruit;
public string Fruit
{
get { return _Fruit; }
set { _Fruit = value; }
}
}
}
我可以直接在顶级类下添加类似以下内容
private string _Fruit;
public string Fruit
{
get { return _Fruit; }
set { _Fruit = value; }
}
并避免数组嵌套吗?
我的目标是反序列化以下格式的 xml
<Basket>
<Fruit>Apple</Fruit>
<Fruit>Orange</Fruit>
<Fruit>Grapes</Fruit>
</Basket>
I want to deserialize an xml file which has to be just in this form
<Basket>
<Fruit>Apple</Fruit>
<Fruit>Orange</Fruit>
<Fruit>Grapes</Fruit>
</Basket>
Out of the examples I read on internet the least possible format I could find was the following
<Basket>
<FruitArray>
<Fruit>Apple</Fruit>
</FruitArray>
<FruitArray>
<Fruit>Orange</Fruit>
</FruitArray>
<FruitArray>
<Fruit>Grapes</Fruit>
</FruitArray>
</Basket>
and that has the following deserialization class for converting it into a class object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XMLSerialization_Basket
{
[System.Xml.Serialization.XmlRootAttribute("Basket", Namespace = "BasketNamespace", IsNullable = false)]
public class Basket
{
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FruitArray")]
public FruitArray[] objFruitArray;
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "BasketNamespace")]
public class FruitArray
{
/// <remarks/>
private string _Fruit;
public string Fruit
{
get { return _Fruit; }
set { _Fruit = value; }
}
}
}
Can I add something like the following directly under top class
private string _Fruit;
public string Fruit
{
get { return _Fruit; }
set { _Fruit = value; }
}
and avoid the array nesting?
my goal is to deserialize an xml of following format
<Basket>
<Fruit>Apple</Fruit>
<Fruit>Orange</Fruit>
<Fruit>Grapes</Fruit>
</Basket>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
恕我直言,我会修改我的 XML 和对象以反序列化为以下内容。
这是 C#(应该编译;))
这是 XML
With all respect I would modify my XML and object to deserialize into to the following.
Here is the C# (Should compile ;))
Here is the XML
你应该只需要:
You should just need:
或者使用 LINQ to XML:
Or using LINQ to XML:
上一篇文章绝对有效...
我组装了以下测试设备,它能够毫无问题地读取所述 XML。
The previous post absolutely worked...
I put together the following test rig and it was able to read the stated XML with no problem.
xml 架构应如下所示:
如果您保存 xsd(我们将其称为 example.xsd)并使用以下参数运行 xsd:
您将拥有此类:
这是一个要演示的示例程序:
它将序列化为此:
This is what the xml schema should look like:
If you save the xsd (let's just call it sample.xsd) and run xsd with the following parameters:
You will have this class:
Here's a sample program to demonstrate:
Which will serialize to this: