C# 无法从XML反序列化数组

C# 无法从XML反序列化数组,c#,xml,C#,Xml,要反序列化XML文件,如 <?xml version="1.0" encoding="utf-8"?> <ProbeEntries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Version>1</Version> <Entries> <ProbeEntry&

要反序列化XML文件,如

<?xml version="1.0" encoding="utf-8"?>
<ProbeEntries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Version>1</Version>
  <Entries>
    <ProbeEntry>
      <FQDN>ns.foo.net</FQDN>
      <Answers>
        <string>20.7.19.1</string>
        <string>20.7.19.2</string>
      </Answers>
    </ProbeEntry>
  </Entries>
</ProbeEntries>

1.
ns.foo.net
20.7.19.1
20.7.19.2
几个类别的定义如下:

namespace TestXML
{
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute]
    [System.Diagnostics.DebuggerStepThroughAttribute]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [XmlTypeAttribute(AnonymousType = true)]
    [XmlRootAttribute(Namespace = "", IsNullable = false)]
    public class ProbeEntries
    {
        private ProbeEntriesEntriesProbeEntry[] entriesField;

        [XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [XmlArrayItemAttribute("ProbeEntry", typeof(ProbeEntriesEntriesProbeEntry), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
        public ProbeEntriesEntriesProbeEntry[] Entries
        {
            get
            {
                return this.entriesField;
            }
            set
            {
                this.entriesField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute]
    [System.Diagnostics.DebuggerStepThroughAttribute]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [XmlTypeAttribute(AnonymousType = true)]
    public class ProbeEntriesEntriesProbeEntry
    {
        private ProbeEntriesEntriesProbeEntryAnswers[] answersField;

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

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute]
    [System.Diagnostics.DebuggerStepThroughAttribute]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [XmlTypeAttribute(AnonymousType = true)]
    public class ProbeEntriesEntriesProbeEntryAnswers
    {
        private string stringField;

        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, ElementName = "string")]
        public string String
        {
            get
            {
                return this.stringField;
            }
            set
            {
                this.stringField = value;
            }
        }
    }

    public static class ProbesReader
    {
        public static ProbeEntries Read(string fileName)
        {

            FileStream readFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            XmlSerializer serializerObj = new XmlSerializer(typeof(ProbeEntries));
            // Load the object saved above by using the Deserialize function
            ProbeEntries loadedObj = (ProbeEntries)serializerObj.Deserialize(readFileStream);
            // Cleanup
            readFileStream.Close();
            return loadedObj;
        }
    }
}
namespace TestXML
{
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“2.0.50727.3038”)]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[XmlTypeAttribute(AnonymousType=true)]
[XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共类问题
{
私人ProbeentriesProbeentry[]Entries字段;
[XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[XmlArrayItemAttribute(“ProbeEntry”,typeof(ProbeEntriesEntriesProbeEntry),Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=false]
公共ProbeentriesProbeEntry[]条目
{
收到
{
返回此.entriesField;
}
设置
{
this.entriesField=值;
}
}
}
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“2.0.50727.3038”)]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[XmlTypeAttribute(AnonymousType=true)]
公共类ProbeentriesProbeentry
{
私人问题调查问卷答案[]答案字段;
/// 
[xmlementAttribute(“Answers”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共问题调查问卷调查答案[]答案
{
收到
{
返回此。应答器字段;
}
设置
{
this.answersField=值;
}
}
}
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“2.0.50727.3038”)]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[XmlTypeAttribute(AnonymousType=true)]
公共类问题调查问卷答案
{
私有字符串字符串字段;
[xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,ElementName=“string”)]
公共字符串
{
收到
{
返回此.stringField;
}
设置
{
this.stringField=值;
}
}
}
公共静态类ProbesReader
{
公共静态ProbeEntries读取(字符串文件名)
{
FileStream readFileStream=新文件流(文件名,FileMode.Open,FileAccess.Read,FileShare.Read);
XmlSerializer serializerObj=新的XmlSerializer(typeof(ProbeEntries));
//使用反序列化函数加载上面保存的对象
ProbeEntries loadedObj=(ProbeEntries)serializerObj.Deserialize(readFileStream);
//清理
readFileStream.Close();
返回加载对象;
}
}
}

但是当使用ProbesReader.Read读取XML时,对于元素,我希望有两个:一个是20.7.19.1,另一个是20.7.19.2。但只有第一个被返回,第二个丢失。我想知道我的XML文件的类定义有什么问题?

您的类没有处理XML中的数据,您将不得不更改一些内容

首先,您需要
XmlArrayAttribute
XmlArrayItemAttribute
上的
XmlArrayItemAttribute
属性。
XmlArrayItemAttribute
必须像在xml中一样命名为
string

public class ProbeEntriesEntriesProbeEntry
{
    private ProbeEntriesEntriesProbeEntryAnswers[] answersField;

    /// <remarks/>
   [XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
   [XmlArrayItemAttribute("string", typeof(ProbeEntriesEntriesProbeEntryAnswers), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public ProbeEntriesEntriesProbeEntryAnswers[] Answers
    {
        get
        {
            return this.answersField;
        }
        set
        {
            this.answersField = value;
        }
    }
}
这将根据您提供的xml示例重新运行所需的多个结果

项目1:

项目2:


替换
问题调查问卷答案

为了

//
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“2.0.50727.3038”)]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[XmlTypeAttribute(AnonymousType=true)]
公共类问题调查问卷答案
{
私有字符串[]字符串字段;
[xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,ElementName=“string”)]
公共字符串[]字符串
{
收到
{
返回此.stringField;
}
设置
{
this.stringField=值;
}
}
}

您需要一个字符串数组,因为不止一个

这个问题与XML有什么关系?@Fresh,因为这是一个关于XML的问题serialization@sa_ddam213当作者发布他们的问题时,最初没有关于XML的代码。它已经更新了,所以现在一切都好了!这两个字符串是属于1
probeentriesProbeentriesProbeentryAnswers
还是2
probeentriesProbeentryAnswers
各有一个字符串?这也是我的第一印象,但我不确定这将如何工作,因为
答案中有多个
probeentriesProbeentryAnswers
,我不确定每个字符串是否属于一个单独的
ProbeentriesProbeentryAnswers
,或者它们是否都属于一个字符串,我想我们中的一个人有正确的答案:)
public class ProbeEntriesEntriesProbeEntryAnswers
{
    private string stringField;

    [XmlText]
    public string String
    {
        get
        {
            return this.stringField;
        }
        set
        {
            this.stringField = value;
        }
    }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
public class ProbeEntriesEntriesProbeEntryAnswers
{
    private string[] stringField;

    [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, ElementName = "string")]
    public string[] String
    {
        get
        {
            return this.stringField;
        }
        set
        {
            this.stringField = value;
        }
    }
}