XML 反序列化为 XSD 生成的类时出现问题

发布于 2024-08-04 20:09:32 字数 1997 浏览 5 评论 0 原文

我有一个相当详细的 xml 文件。下面是顶级节点(我已经包含了椭圆,因为较低级别的节点都格式良好并且正确填充了数据):

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <Models>...</Models>
    <Data>...</Data>
</config>

我已经使用 Visual Studio 2008 命令提示符创建了一个 xsd 文件:

xsd sample.xml

这会生成 xsd 文件。然后,我使用以下命令从 xsd 自动生成类:

xsd sample.xsd /classes

为了将 xml 文件反序列化为类对象,我在帮助程序类中使用读取函数:

public class XmlSerializerHelper<T>
{
    public Type _type;

    public XmlSerializerHelper()
    {
        _type = typeof(T);
    }

    public void Save(string path, object obj)
    {
        using (TextWriter textWriter = new StreamWriter(path))
        {
            XmlSerializer serializer = new XmlSerializer(_type);
            serializer.Serialize(textWriter, obj);
        }
    }

    public T Read(string path)
    {
        T result;
        using (TextReader textReader = new StreamReader(path))
        {
            XmlSerializer deserializer = new XmlSerializer(_type);
            result = (T)deserializer.Deserialize(textReader);
        }
        return result;
    }
}

尝试反序列化时:

var helper = new XmlSerializerHelper<configModels>();
var obj = new configModels();
obj = helper.Read(filepath);

我收到一个我推断出的错误是因为反序列化器正在寻找“Models”节点,但相应的类名是作为根节点和“Model”节点(configModels)的组合生成的。为什么类名会这样生成呢?

我尝试使用以下方法从顶部节点进行反序列化:

var helper = new XmlSerializerHelper<config>();
var obj = new config();
obj = helper.Read(filepath);

不幸的是,这会导致一系列错误,如下所示:

System.InvalidOperationException was unhandled by user code
Message="Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'Application.Lease[]' to 'Application.Lease'
error CS0030: Cannot convert type 'Application.CashFlow[]' to 'Application.CashFlow'
...ect.

有人可以引导我解决我的 xsd 自动生成可能做错的事情吗?

I have a rather detailed xml file. Below is the top level nodes (I have included the ellipse as the lower level nodes are all well formed and properly filled with data):

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <Models>...</Models>
    <Data>...</Data>
</config>

I have created an xsd file from using the Visual Studio 2008 command prompt:

xsd sample.xml

This generates the xsd file just fine. I then auto generate classes from the xsd with the command:

xsd sample.xsd /classes

For the deserialization of the xml file into a class object, I'm using the read function in the helper class:

public class XmlSerializerHelper<T>
{
    public Type _type;

    public XmlSerializerHelper()
    {
        _type = typeof(T);
    }

    public void Save(string path, object obj)
    {
        using (TextWriter textWriter = new StreamWriter(path))
        {
            XmlSerializer serializer = new XmlSerializer(_type);
            serializer.Serialize(textWriter, obj);
        }
    }

    public T Read(string path)
    {
        T result;
        using (TextReader textReader = new StreamReader(path))
        {
            XmlSerializer deserializer = new XmlSerializer(_type);
            result = (T)deserializer.Deserialize(textReader);
        }
        return result;
    }
}

When attempting the deserialization with:

var helper = new XmlSerializerHelper<configModels>();
var obj = new configModels();
obj = helper.Read(filepath);

I receive an error that I have deduced is because the deserializer is looking for the 'Models' node but the corresponding class name was generated as a combination of the root node and the 'Model' node (configModels). Why are the class names generated like this?

I tried to deserialize from the top node using:

var helper = new XmlSerializerHelper<config>();
var obj = new config();
obj = helper.Read(filepath);

Unfortunately, this the results in a slew of errors like the following:

System.InvalidOperationException was unhandled by user code
Message="Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'Application.Lease[]' to 'Application.Lease'
error CS0030: Cannot convert type 'Application.CashFlow[]' to 'Application.CashFlow'
...ect.

Can somebody steer me towards what I might be doing wrong with my xsd auto-generating?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情徒 2024-08-11 20:09:32

XSD.EXE 是一个好的开始 - 但它还远非完美。另外,根据您提供的 XML,XSD.EXE 无法始终确定某物是对象的单个实例还是开放式对象数组。

这似乎是您的两个元素的情况 - Application.LeaseApplication.CashFlow。它们在生成的 XSD 文件中是如何定义的?这对你来说有意义吗?很可能,您必须添加一些提示,例如:

<xs:element name="Lease" minOccurs="0" maxOccurs="1" />

对于可选属性,仅出现零次或一次。对于 xsd.exe 工具来说,仅根据单个 XML 示例文件就很难弄清楚类似的事情。

马克

XSD.EXE is a good start - but it's far from perfect. Also, based on the XML you provided, XSD.EXE can't always decide for sure whether something is a single instance of an object, or an open-ended array of objects.

This seems to be the case for your two elements - Application.Lease and Application.CashFlow. How are they defined in the generated XSD file? Does that make sense to you? Quite possibly, you'd have to add a little hints, such as:

<xs:element name="Lease" minOccurs="0" maxOccurs="1" />

for an optional property, that's zero or one occurences only. Things like that are really hard for the xsd.exe tool to figure out based on just a single XML sample file.

Marc

把梦留给海 2024-08-11 20:09:32

转到生成的类并从 [][] ---> 更改所有内容[]

Go to your generated class and change all from [][] ---> []

救赎№ 2024-08-11 20:09:32

xsd.exe 和列表存在问题。您必须进入生成的类并手动将文件编辑为正确的类型。我已改用 Xsd2Code。目前看来还没有这个问题。

There's an issue with xsd.exe and lists. You have to go into the generated class and manually edit the file to the correct type. I've switched to using Xsd2Code. So far it doesn't seem to have this problem.

ぃ弥猫深巷。 2024-08-11 20:09:32

可能导致此问题的另一个问题是标签之间的 xml 文件内容(即内容)在不应该编码的情况下仍然进行了编码。例如,我的内容中的
标签仍然是
而不是
。 xsd 生成器将这些元素转换为模式中的元素,然后将它们错误地标记为无界,因为发现了多个元素。对它们进行取消编码解决了问题并正确生成了类。

Another issue that can cause this problem is that the xml file contents between the tags (meaning the content) is still encoded when it shouldn't be. For example, the <br> tags in my content were still <br> instead of <br />. The xsd generator turned these into elements in the schema then mislabeled them as unbounded since there was more than one found. Unencoding them fixed the problem and generated the classes correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文