3

使用XSD.EXE我生成了一个 *.cs 文件来序列化 XML。当我序列化我得到错误

"The same table 'DefaultFont' cannot be the child table in two nested relations."

为什么我会收到错误消息?我可以在不使用 XSLT 转换的情况下修复错误吗?

序列化代码

        StreamReader fs = new StreamReader(@"C:\path\DocumentSample.xml");
        XmlSerializer serializer = new XmlSerializer(typeof(PhysDocDocument));
        var result = serializer.Deserialize(fs) as PhysDocDocument;//exception thrown here

示例 XML

<PhysDocDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <PhysDocNode HeaderVisible="true" Display="Summary" IsCollapsed="false" CopyForwardEnabled="true" IsHidden="false" Borders="false">
        <LinkedPluginId>00000000-0000-0000-0000-000000000000</LinkedPluginId>
        <Plugin xsi:type="PromptPlugin" ID="2446441e-6eb2-49ef-b8e3-28f638755b75">
            <CopyForwardChecked>true</CopyForwardChecked>
            <PromptBase xsi:type="MemoPrompt">
                <Label>Comment</Label>
                <DocumentValue>
                    <Paragraphs />
                    <DefaultFont FontFamily="Arial" SizeInPoints="10" Style="Regular" />
                </DocumentValue>
                <ShowLabel>false</ShowLabel>
                <RenderHeader>false</RenderHeader>
            </PromptBase>
        </Plugin>
    </PhysDocNode>
</PhysDocDocument>

生成的代码

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.17929
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class PhysDocDocument
{

    private PhysDocDocumentPhysDocNode[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("PhysDocNode", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNode[] Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNode
{

    private string linkedPluginIdField;

    private PhysDocDocumentPhysDocNodePlugin[] pluginField;

    private string headerVisibleField;

    private string displayField;

    private string isCollapsedField;

    private string copyForwardEnabledField;

    private string isHiddenField;

    private string bordersField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string LinkedPluginId
    {
        get
        {
            return this.linkedPluginIdField;
        }
        set
        {
            this.linkedPluginIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Plugin", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNodePlugin[] Plugin
    {
        get
        {
            return this.pluginField;
        }
        set
        {
            this.pluginField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string HeaderVisible
    {
        get
        {
            return this.headerVisibleField;
        }
        set
        {
            this.headerVisibleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Display
    {
        get
        {
            return this.displayField;
        }
        set
        {
            this.displayField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string IsCollapsed
    {
        get
        {
            return this.isCollapsedField;
        }
        set
        {
            this.isCollapsedField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string CopyForwardEnabled
    {
        get
        {
            return this.copyForwardEnabledField;
        }
        set
        {
            this.copyForwardEnabledField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string IsHidden
    {
        get
        {
            return this.isHiddenField;
        }
        set
        {
            this.isHiddenField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Borders
    {
        get
        {
            return this.bordersField;
        }
        set
        {
            this.bordersField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePlugin
{

    private string copyForwardCheckedField;

    private PhysDocDocumentPhysDocNodePluginPromptBase[] promptBaseField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string CopyForwardChecked
    {
        get
        {
            return this.copyForwardCheckedField;
        }
        set
        {
            this.copyForwardCheckedField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("PromptBase", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNodePluginPromptBase[] PromptBase
    {
        get
        {
            return this.promptBaseField;
        }
        set
        {
            this.promptBaseField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBase
{

    private string labelField;

    private string showLabelField;

    private string renderHeaderField;

    private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] documentValueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Label
    {
        get
        {
            return this.labelField;
        }
        set
        {
            this.labelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string ShowLabel
    {
        get
        {
            return this.showLabelField;
        }
        set
        {
            this.showLabelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string RenderHeader
    {
        get
        {
            return this.renderHeaderField;
        }
        set
        {
            this.renderHeaderField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("DocumentValue", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] DocumentValue
    {
        get
        {
            return this.documentValueField;
        }
        set
        {
            this.documentValueField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue
{

    private string paragraphsField;

    private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] defaultFontField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Paragraphs
    {
        get
        {
            return this.paragraphsField;
        }
        set
        {
            this.paragraphsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("DefaultFont", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] DefaultFont
    {
        get
        {
            return this.defaultFontField;
        }
        set
        {
            this.defaultFontField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont
{

    private string fontFamilyField;

    private string sizeInPointsField;

    private string styleField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string FontFamily
    {
        get
        {
            return this.fontFamilyField;
        }
        set
        {
            this.fontFamilyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string SizeInPoints
    {
        get
        {
            return this.sizeInPointsField;
        }
        set
        {
            this.sizeInPointsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Style
    {
        get
        {
            return this.styleField;
        }
        set
        {
            this.styleField = value;
        }
    }
}
4

1 回答 1

0

您生成的一些类在 XmlTypeAttribute 中设置了 AnonymousType=true。当我修改它并添加真实的类名时 - 错误消失了。这是完整的工作示例(请注意,我在 XmlTypeAttribute 中添加了“MemoPrompt”和“PromptPlugin”类名):

using System.Xml.Serialization;
using System.IO;
using System;

class App
{
  static void Main()
  {
    StreamReader fs = new StreamReader(@"sample.xml");
    XmlSerializer serializer = new XmlSerializer(typeof(PhysDocDocument));
    var result = serializer.Deserialize(fs) as PhysDocDocument;  
    Console.WriteLine(result);
  }
}


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class PhysDocDocument
{

    private PhysDocDocumentPhysDocNode[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("PhysDocNode", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNode[] Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNode
{

    private string linkedPluginIdField;

    private PhysDocDocumentPhysDocNodePlugin[] pluginField;

    private string headerVisibleField;

    private string displayField;

    private string isCollapsedField;

    private string copyForwardEnabledField;

    private string isHiddenField;

    private string bordersField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string LinkedPluginId
    {
        get
        {
            return this.linkedPluginIdField;
        }
        set
        {
            this.linkedPluginIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Plugin", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNodePlugin[] Plugin
    {
        get
        {
            return this.pluginField;
        }
        set
        {
            this.pluginField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string HeaderVisible
    {
        get
        {
            return this.headerVisibleField;
        }
        set
        {
            this.headerVisibleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Display
    {
        get
        {
            return this.displayField;
        }
        set
        {
            this.displayField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string IsCollapsed
    {
        get
        {
            return this.isCollapsedField;
        }
        set
        {
            this.isCollapsedField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string CopyForwardEnabled
    {
        get
        {
            return this.copyForwardEnabledField;
        }
        set
        {
            this.copyForwardEnabledField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string IsHidden
    {
        get
        {
            return this.isHiddenField;
        }
        set
        {
            this.isHiddenField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Borders
    {
        get
        {
            return this.bordersField;
        }
        set
        {
            this.bordersField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute("PromptPlugin")]
public partial class PhysDocDocumentPhysDocNodePlugin
{

    private string copyForwardCheckedField;

    private PhysDocDocumentPhysDocNodePluginPromptBase[] promptBaseField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string CopyForwardChecked
    {
        get
        {
            return this.copyForwardCheckedField;
        }
        set
        {
            this.copyForwardCheckedField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("PromptBase", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNodePluginPromptBase[] PromptBase
    {
        get
        {
            return this.promptBaseField;
        }
        set
        {
            this.promptBaseField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ID
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute("MemoPrompt")]
public partial class PhysDocDocumentPhysDocNodePluginPromptBase
{

    private string labelField;

    private string showLabelField;

    private string renderHeaderField;

    private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] documentValueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Label
    {
        get
        {
            return this.labelField;
        }
        set
        {
            this.labelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string ShowLabel
    {
        get
        {
            return this.showLabelField;
        }
        set
        {
            this.showLabelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string RenderHeader
    {
        get
        {
            return this.renderHeaderField;
        }
        set
        {
            this.renderHeaderField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("DocumentValue", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] DocumentValue
    {
        get
        {
            return this.documentValueField;
        }
        set
        {
            this.documentValueField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue
{

    private string paragraphsField;

    private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] defaultFontField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Paragraphs
    {
        get
        {
            return this.paragraphsField;
        }
        set
        {
            this.paragraphsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("DefaultFont", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] DefaultFont
    {
        get
        {
            return this.defaultFontField;
        }
        set
        {
            this.defaultFontField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont
{

    private string fontFamilyField;

    private string sizeInPointsField;

    private string styleField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string FontFamily
    {
        get
        {
            return this.fontFamilyField;
        }
        set
        {
            this.fontFamilyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string SizeInPoints
    {
        get
        {
            return this.sizeInPointsField;
        }
        set
        {
            this.sizeInPointsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Style
    {
        get
        {
            return this.styleField;
        }
        set
        {
            this.styleField = value;
        }
    }
}
于 2013-04-23T20:19:28.217 回答