c# - XmlSerializer 不填充值

标签 c# xml serialization xsd xml-serialization

我从 DTD(通过 XSD 和 xsd.exe)为我的 C# 项目创建了类,因此我可以轻松地将它们反序列化为代码中的模型类。

我大致是这样做的:

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.DtdProcessing = DtdProcessing.Parse;

XmlReader reader = XmlReader.Create(tipsfile.FullName, readerSettings);

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Tips";
xRoot.IsNullable = true;

XmlSerializer serializer = new XmlSerializer(typeof(Tips), xRoot);
Tips tips = (Tips)serializer.Deserialize(reader);

reader.Close();

但是,在检查 tips 后,我发现它根本不包含原始 XML 文件中的任何值。另外,我尝试在 Tips 属性的 set-Body 上设置断点,但从未达到该断点,尽管我确信它在原始 XML 文件。

为什么文件没有正确反序列化到类中?我的代码中缺少什么吗?

编辑:这是 Tips.cs 文件,它是从 XSD 自动生成的

using System.Xml.Serialization;

namespace MyNs.Model
{
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/caravan_1")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/caravan_1", IsNullable = false)]
    public partial class Tips
    {
        private Chapter[] chapterField;

        [System.Xml.Serialization.XmlElementAttribute("Chapter")]
        public Chapter[] Chapter
        {
            get
            {
                return this.chapterField;
            }
            set
            {
                this.chapterField = value;
            }
        }
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/caravan_1")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/caravan_1", IsNullable = false)]
    public partial class Chapter
    {
        private string headingField;
        private CarType[] carTypesField;
        private Section[] sectionField;
        private string countryField;
        private string languageField;

        public string Heading
        {
            get
            {
                return this.headingField;
            }
            set
            {
                this.headingField = value;
            }
        }

        [System.Xml.Serialization.XmlArrayItemAttribute("CarType", IsNullable = false)]
        public CarType[] CarTypes
        {
            get
            {
                return this.carTypesField;
            }
            set
            {
                this.carTypesField = value;
            }
        }

        [System.Xml.Serialization.XmlElementAttribute("Section")]
        public Section[] Section
        {
            get
            {
                return this.sectionField;
            }
            set
            {
                this.sectionField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Country
        {
            get
            {
                return this.countryField;
            }
            set
            {
                this.countryField = value;
            }
        }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string Language
        {
            get
            {
                return this.languageField;
            }
            set
            {
                this.languageField = value;
            }
        }
    }
     [... and so on ...]

还有一个示例 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Tips SYSTEM "caravan_1.dtd">
<Tips>
    <Chapter Country="dafghagt" Language="de">
        <Heading>fgagasgargaergarg</Heading>        
        <Section id="1">
            <Heading>afdhwr6u5taehtaqh5</Heading>
            <SubSection id="1">
                <Heading>46k46kw6jhadfgadfha</Heading>              
                <Table>
                    <Row id="1">
                        <Heading>sgjsfgjsgfh443q572q356</Heading>
                        <Items>
                            <Item car="motor1" id="1">
                                <BodyText color="red">130*</BodyText>
                                <Subscript>3</Subscript>
                            </Item>
                        </Items>
                    </Row>
                </Table>
            </SubSection>
        </Section>
    </Chapter>
</Tips>

最佳答案

        StreamReader sw = new StreamReader(fileName, false);
        XmlTextReader xmlw = new XmlTextReader(sw);
        XmlSerializer writer = new XmlSerializer(
            type,
            GetOverrides(),
            extraTypes,
            null,
            null);

        object o = writer.Deserialize(xmlw);

根据我的评论,get overrides 只是一个包含类似于您的 xml 根属性的方法,如果需要,我也可以向您展示这一点

编辑: 额外类型的示例

    public Type[] GetTypes()
    {
        return new Type[] { typeof(Class1), typeof(Class2)};
    }

EDIT2:获取覆盖返回(注意这是未经测试的)

XmlAttributes attribs = new XmlAttributes();
//attribs.XmlRoot - can edit root here (instead of xmlrootattribute)
attribs.XmlElements.Add(myAttribute);

XmlAttributeOverrides myOverride = new XmlAttributeOverrides();
myOverride.Add(typeof(Tips), "Tips", attribs);
return myOverride

关于c# - XmlSerializer 不填充值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15070745/

相关文章:

不同元素中相同属性的 XML 值不能重复

xml - 用另一个 Node 实例替换 XML Node

java - 节点流是什么意思?

json - 使用 Newtonsoft JSON 反序列化的 System.TypeLoadException

c# - Xamarin。加载mp3以通过MediaPlayer播放时遇到问题:FileNotFoundException

c# - 带有 OUT 参数的 Webmatrix/Razor 和存储过程

Java - 从剪贴板检索 XML (Excel) 电子表格

java - 序列化字符串而不转义 "to\"

c# - 数据绑定(bind) : does not contain a property with the name 'CategoryId' error

c# - .NET 中的类可以继承自 LambdaExpression 吗?还是不推荐这样做?