<legend id='mOAsP'><style id='mOAsP'><dir id='mOAsP'><q id='mOAsP'></q></dir></style></legend>

<small id='mOAsP'></small><noframes id='mOAsP'>

<i id='mOAsP'><tr id='mOAsP'><dt id='mOAsP'><q id='mOAsP'><span id='mOAsP'><b id='mOAsP'><form id='mOAsP'><ins id='mOAsP'></ins><ul id='mOAsP'></ul><sub id='mOAsP'></sub></form><legend id='mOAsP'></legend><bdo id='mOAsP'><pre id='mOAsP'><center id='mOAsP'></center></pre></bdo></b><th id='mOAsP'></th></span></q></dt></tr></i><div id='mOAsP'><tfoot id='mOAsP'></tfoot><dl id='mOAsP'><fieldset id='mOAsP'></fieldset></dl></div>
    • <bdo id='mOAsP'></bdo><ul id='mOAsP'></ul>

    <tfoot id='mOAsP'></tfoot>

    1. 将 XML 反序列化为 XSD 生成的类的问题

      时间:2023-06-02
    2. <tfoot id='MtqUf'></tfoot>
      1. <i id='MtqUf'><tr id='MtqUf'><dt id='MtqUf'><q id='MtqUf'><span id='MtqUf'><b id='MtqUf'><form id='MtqUf'><ins id='MtqUf'></ins><ul id='MtqUf'></ul><sub id='MtqUf'></sub></form><legend id='MtqUf'></legend><bdo id='MtqUf'><pre id='MtqUf'><center id='MtqUf'></center></pre></bdo></b><th id='MtqUf'></th></span></q></dt></tr></i><div id='MtqUf'><tfoot id='MtqUf'></tfoot><dl id='MtqUf'><fieldset id='MtqUf'></fieldset></dl></div>
          <tbody id='MtqUf'></tbody>
          <legend id='MtqUf'><style id='MtqUf'><dir id='MtqUf'><q id='MtqUf'></q></dir></style></legend>

          <small id='MtqUf'></small><noframes id='MtqUf'>

            <bdo id='MtqUf'></bdo><ul id='MtqUf'></ul>

              1. 本文介绍了将 XML 反序列化为 XSD 生成的类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

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

                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>
                

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

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

                xsd sample.xml
                

                这会生成 xsd 文件就好了.然后我使用以下命令从 xsd 自动生成类:

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

                xsd sample.xsd /classes
                

                为了将xml文件反序列化为类对象,我在helper类中使用了read函数:

                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;
                    }
                }
                

                尝试反序列化时:

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

                我收到一个错误,我推断是因为反序列化器正在寻找模型"节点,但相应的类名是作为根节点和模型"节点 (configModels) 的组合生成的.为什么生成的类名是这样的?

                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.
                

                有人可以指导我解决我的 xsd 自动生成可能做错的地方吗?

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

                推荐答案

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

                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.

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

                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" />
                

                对于可选属性,仅出现 0 次或 1 次.对于 xsd.exe 工具来说,仅基于单个 XML 示例文件,这样的事情真的很难弄清楚.

                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.

                马克

                这篇关于将 XML 反序列化为 XSD 生成的类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:app.config “找不到架构信息"转换为 Visual Stu 下一篇:如何解析 XSD 以从 &lt;xsd:simpleType&gt; 获取信

                相关文章

                最新文章

                  <i id='wu4IG'><tr id='wu4IG'><dt id='wu4IG'><q id='wu4IG'><span id='wu4IG'><b id='wu4IG'><form id='wu4IG'><ins id='wu4IG'></ins><ul id='wu4IG'></ul><sub id='wu4IG'></sub></form><legend id='wu4IG'></legend><bdo id='wu4IG'><pre id='wu4IG'><center id='wu4IG'></center></pre></bdo></b><th id='wu4IG'></th></span></q></dt></tr></i><div id='wu4IG'><tfoot id='wu4IG'></tfoot><dl id='wu4IG'><fieldset id='wu4IG'></fieldset></dl></div>

                    <bdo id='wu4IG'></bdo><ul id='wu4IG'></ul>

                1. <tfoot id='wu4IG'></tfoot>

                2. <legend id='wu4IG'><style id='wu4IG'><dir id='wu4IG'><q id='wu4IG'></q></dir></style></legend>

                    <small id='wu4IG'></small><noframes id='wu4IG'>