XML-序列化后无法进行审理[英] XML - Can not deserialize after serialize

本文是小编为大家收集整理的关于XML-序列化后无法进行审理的处理方法,想解了XML-序列化后无法进行审理的问题怎么解决?XML-序列化后无法进行审理问题的解决办法?XML-序列化后无法进行审理问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我创建一个列表,将其保存为XML(带有XMLSerializer),但我并不成功(尽管所有Web搜索…)都无法进行.

我的实体是:

public class basicTxtFile
{
    public string filename;
    public string description;
}

public class fileTools
{
};

public class textboxTool : fileTools    // text box
{
    public string defaultText;
    public bool multiLine;
    public bool browseButton;
};

public class comboboxTool : fileTools   // combo box
{
    public List<string> values = new List<string>();
};

// Must file, can choose tools: textbox and\or combobox
public class mustFiles : basicTxtFile
{
    public List<fileTools> mustTools = new List<fileTools>();
};

public class OptionalFiles : mustFiles
{
    public bool exist;  // checkbox for defualt value - if the file is exist, if not.
};

在我的应用程序中,我会板条列表,然后手动填写. 之后,我用此代码保存了它:

//  Save list into XML  -   success
XmlSerializer serializer = new XmlSerializer(typeof(List<mustFiles>), new Type[] {typeof(fileTools), typeof(textboxTool), typeof(comboboxTool)});

using (FileStream stream = File.OpenWrite("MustFiles.xml"))
{
    serializer.Serialize(stream, mustTxtFiles);
}

然后,我尝试将XML文件加载到列表中,但是由于以下原因是失败的:" XML文档中存在错误(2,2)." 和_ innerexception ="没有预期的." 尽管 XML文件会自动生成.

我的负载代码是:

// Load XML file into list
List<mustFiles> mustTry = new List<mustFiles>();
mustTry = bl.loadXmlIntoList<mustFiles>("MustFiles.xml", "mustFiles");

loadXmlintolist函数:

public List<T> loadXmlIntoList<T>(string xmlFileName, string xmlElemnetName)
{
    XmlRootAttribute xRoot = new XmlRootAttribute();
    xRoot.ElementName = xmlElemnetName;
    xRoot.IsNullable = true;

    XmlSerializer serializer = new XmlSerializer(typeof(T), xRoot);

    using (FileStream stream = File.OpenRead(xmlFileName))
    {
        List<T> dezerializedList = (List<T>)serializer.Deserialize(stream);
        return dezerializedList;
    }
}

我的问题:我做错了什么?如何将XML文件加载到列表中?

谢谢!

XML文件(自动生成)看起来像这样:

<?xml version="1.0"?>
<ArrayOfMustFiles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <mustFiles>
    <filename>file1.txt</filename>
    <description>desc1</description>
    <mustTools>
      <fileTools xsi:type="textboxTool">
        <defaultText>Default text 01</defaultText>
        <multiLine>false</multiLine>
        <browseButton>false</browseButton>
      </fileTools>
    </mustTools>
  </mustFiles>
  <mustFiles>
    <filename>file2.txt</filename>
    <description>desc2</description>
    <mustTools>
      <fileTools xsi:type="textboxTool">
        <defaultText>Defualt text 02</defaultText>
        <multiLine>true</multiLine>
        <browseButton>true</browseButton>
      </fileTools>
      <fileTools xsi:type="comboboxTool">
        <values>
          <string>Val1</string>
          <string>Val2</string>
          <string>Val3</string>
        </values>
      </fileTools>
    </mustTools>
  </mustFiles>
  <mustFiles>
    <filename>file2.txt</filename>
    <description>desc2</description>
    <mustTools>
      <fileTools xsi:type="textboxTool">
        <defaultText>Defualt text 03</defaultText>
        <multiLine>false</multiLine>
        <browseButton>true</browseButton>
      </fileTools>
      <fileTools xsi:type="comboboxTool">
        <values>
          <string>ComboVal 1</string>
          <string>ComboVal  2</string>
          <string>ComboVal  3</string>
        </values>
      </fileTools>
      <fileTools xsi:type="comboboxTool">
        <values>
          <string>Second ComboVal 1</string>
          <string>Second ComboVal  2</string>
          <string>Second ComboVal  3</string>
        </values>
      </fileTools>
      <fileTools xsi:type="textboxTool">
        <defaultText>Second defualt text 03</defaultText>
        <multiLine>true</multiLine>
        <browseButton>false</browseButton>
      </fileTools>
    </mustTools>
  </mustFiles>
</ArrayOfMustFiles>

更新:我也尝试向实体添加{get; set;},例如:

public class basicTxtFile
{
    public string filename{ set; get; }
    public string description{ set; get; }
}

public class fileTools
{ };

public class textboxTool : fileTools
{
    public string defaultText{ set; get; }
    public bool multiLine{ set; get; }
    public bool browseButton{ set; get; }
};

public class comboboxTool : fileTools
{
    public List<string> values { set; get; }
    public comboboxTool()
    {
        values = new List<string>();
    }
};

public class mustFiles : basicTxtFile
{
    public List<fileTools> mustTools { set; get; }
    public mustFiles()
    {
        mustTools = new List<fileTools>();
    }
};

推荐答案

我不是XML专家.您想在LoadXmlintolist()中使用XMLRootAttribute()?

我已经对其进行了稍微重新设计,因此避难代码看起来更像是其序列化:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<mustFiles> mustTxtFiles = new List<mustFiles>();

        mustFiles mf = new mustFiles();
        mf.filename = "filenameA";
        mf.description = "descriptionA";
        textboxTool tbt = new textboxTool();
        tbt.defaultText = "defaultTextA";
        tbt.browseButton = true;
        tbt.multiLine = true;
        mf.mustTools.Add(tbt);
        mustTxtFiles.Add(mf);

        mf = new mustFiles();
        mf.filename = "filenameB";
        mf.description = "descriptionB";
        tbt = new textboxTool();
        tbt.defaultText = "defaultTextB";
        tbt.browseButton = true;
        tbt.multiLine = true;
        mf.mustTools.Add(tbt);
        mustTxtFiles.Add(mf);

        // serialize it
        XmlSerializer serializer = new XmlSerializer(typeof(List<mustFiles>), new Type[] {typeof(fileTools), typeof(textboxTool), typeof(comboboxTool)});
        string xmlFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MustFiles.xml");
        using (System.IO.FileStream stream = File.OpenWrite(xmlFile))
        {
            serializer.Serialize(stream, mustTxtFiles);
        }

        // Why not just this?
        // deserialize it 
        //List<mustFiles> mustTry;
        //using (FileStream stream = File.OpenRead(xmlFile))
        //{
        //    mustTry = (List<mustFiles>)serializer.Deserialize(stream);
        //}

        // deserialize it with generic function:
        List<mustFiles> mustTry = loadXml<List<mustFiles>>(xmlFile, new Type[] { typeof(fileTools), typeof(textboxTool), typeof(comboboxTool) });
    }

    public T loadXml<T>(string xmlFileName, Type[] additionalTypes)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T), additionalTypes);
        using (FileStream stream = File.OpenRead(xmlFileName))
        {
            return (T)serializer.Deserialize(stream);
        }
    }

}

其他推荐答案

我认为您是在缺少get {} set {}属性的方法时引起的,您正在序列化或进行序列化!

如果要在序列化中使用它们,则必须将其用作属性.如果您仅将它们用作字段,没有Get&Set

,就会出现问题

本文地址:https://www.itbaoku.cn/post/2352613.html

问题描述

I create a list, save it as XML (with XmlSerializer) but I not success (although all web searches…) to deserialize.

My entities are:

public class basicTxtFile
{
    public string filename;
    public string description;
}

public class fileTools
{
};

public class textboxTool : fileTools    // text box
{
    public string defaultText;
    public bool multiLine;
    public bool browseButton;
};

public class comboboxTool : fileTools   // combo box
{
    public List<string> values = new List<string>();
};

// Must file, can choose tools: textbox and\or combobox
public class mustFiles : basicTxtFile
{
    public List<fileTools> mustTools = new List<fileTools>();
};

public class OptionalFiles : mustFiles
{
    public bool exist;  // checkbox for defualt value - if the file is exist, if not.
};

In my application I crate a list and I fill it manually. After it I saved it with this code:

//  Save list into XML  -   success
XmlSerializer serializer = new XmlSerializer(typeof(List<mustFiles>), new Type[] {typeof(fileTools), typeof(textboxTool), typeof(comboboxTool)});

using (FileStream stream = File.OpenWrite("MustFiles.xml"))
{
    serializer.Serialize(stream, mustTxtFiles);
}

Then I try to load the xml file into list, but it's failed due to: "There is an error in XML document (2, 2)." and _innerException = " was not expected." although the xml file generate automatically.

My load code is:

// Load XML file into list
List<mustFiles> mustTry = new List<mustFiles>();
mustTry = bl.loadXmlIntoList<mustFiles>("MustFiles.xml", "mustFiles");

loadXmlIntoList function:

public List<T> loadXmlIntoList<T>(string xmlFileName, string xmlElemnetName)
{
    XmlRootAttribute xRoot = new XmlRootAttribute();
    xRoot.ElementName = xmlElemnetName;
    xRoot.IsNullable = true;

    XmlSerializer serializer = new XmlSerializer(typeof(T), xRoot);

    using (FileStream stream = File.OpenRead(xmlFileName))
    {
        List<T> dezerializedList = (List<T>)serializer.Deserialize(stream);
        return dezerializedList;
    }
}

My question: What I did wrong? how can I load the xml file into the list?

Thank you!

The XML file (that generate automatically) looks like this:

<?xml version="1.0"?>
<ArrayOfMustFiles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <mustFiles>
    <filename>file1.txt</filename>
    <description>desc1</description>
    <mustTools>
      <fileTools xsi:type="textboxTool">
        <defaultText>Default text 01</defaultText>
        <multiLine>false</multiLine>
        <browseButton>false</browseButton>
      </fileTools>
    </mustTools>
  </mustFiles>
  <mustFiles>
    <filename>file2.txt</filename>
    <description>desc2</description>
    <mustTools>
      <fileTools xsi:type="textboxTool">
        <defaultText>Defualt text 02</defaultText>
        <multiLine>true</multiLine>
        <browseButton>true</browseButton>
      </fileTools>
      <fileTools xsi:type="comboboxTool">
        <values>
          <string>Val1</string>
          <string>Val2</string>
          <string>Val3</string>
        </values>
      </fileTools>
    </mustTools>
  </mustFiles>
  <mustFiles>
    <filename>file2.txt</filename>
    <description>desc2</description>
    <mustTools>
      <fileTools xsi:type="textboxTool">
        <defaultText>Defualt text 03</defaultText>
        <multiLine>false</multiLine>
        <browseButton>true</browseButton>
      </fileTools>
      <fileTools xsi:type="comboboxTool">
        <values>
          <string>ComboVal 1</string>
          <string>ComboVal  2</string>
          <string>ComboVal  3</string>
        </values>
      </fileTools>
      <fileTools xsi:type="comboboxTool">
        <values>
          <string>Second ComboVal 1</string>
          <string>Second ComboVal  2</string>
          <string>Second ComboVal  3</string>
        </values>
      </fileTools>
      <fileTools xsi:type="textboxTool">
        <defaultText>Second defualt text 03</defaultText>
        <multiLine>true</multiLine>
        <browseButton>false</browseButton>
      </fileTools>
    </mustTools>
  </mustFiles>
</ArrayOfMustFiles>

Update: I also try add {get; set;} to The entities, like this:

public class basicTxtFile
{
    public string filename{ set; get; }
    public string description{ set; get; }
}

public class fileTools
{ };

public class textboxTool : fileTools
{
    public string defaultText{ set; get; }
    public bool multiLine{ set; get; }
    public bool browseButton{ set; get; }
};

public class comboboxTool : fileTools
{
    public List<string> values { set; get; }
    public comboboxTool()
    {
        values = new List<string>();
    }
};

public class mustFiles : basicTxtFile
{
    public List<fileTools> mustTools { set; get; }
    public mustFiles()
    {
        mustTools = new List<fileTools>();
    }
};

推荐答案

I'm not an XML expert. What are you trying to do with the XmlRootAttribute in loadXmlIntoList()?

I've reworked it slightly so that the deserialization code looks more like its serialization counterpart:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<mustFiles> mustTxtFiles = new List<mustFiles>();

        mustFiles mf = new mustFiles();
        mf.filename = "filenameA";
        mf.description = "descriptionA";
        textboxTool tbt = new textboxTool();
        tbt.defaultText = "defaultTextA";
        tbt.browseButton = true;
        tbt.multiLine = true;
        mf.mustTools.Add(tbt);
        mustTxtFiles.Add(mf);

        mf = new mustFiles();
        mf.filename = "filenameB";
        mf.description = "descriptionB";
        tbt = new textboxTool();
        tbt.defaultText = "defaultTextB";
        tbt.browseButton = true;
        tbt.multiLine = true;
        mf.mustTools.Add(tbt);
        mustTxtFiles.Add(mf);

        // serialize it
        XmlSerializer serializer = new XmlSerializer(typeof(List<mustFiles>), new Type[] {typeof(fileTools), typeof(textboxTool), typeof(comboboxTool)});
        string xmlFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MustFiles.xml");
        using (System.IO.FileStream stream = File.OpenWrite(xmlFile))
        {
            serializer.Serialize(stream, mustTxtFiles);
        }

        // Why not just this?
        // deserialize it 
        //List<mustFiles> mustTry;
        //using (FileStream stream = File.OpenRead(xmlFile))
        //{
        //    mustTry = (List<mustFiles>)serializer.Deserialize(stream);
        //}

        // deserialize it with generic function:
        List<mustFiles> mustTry = loadXml<List<mustFiles>>(xmlFile, new Type[] { typeof(fileTools), typeof(textboxTool), typeof(comboboxTool) });
    }

    public T loadXml<T>(string xmlFileName, Type[] additionalTypes)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T), additionalTypes);
        using (FileStream stream = File.OpenRead(xmlFileName))
        {
            return (T)serializer.Deserialize(stream);
        }
    }

}

其他推荐答案

I think your is caused while missing the get{} set{} methods of the properties you are serializing or deserializing!

You have to use it as properties if you want to use them in serialization. there are Problems if you use them only as fields without get & set

查看更多