简体   繁体   English

我试图简单地将 append 两种不同的数据类型放入我的 xml 文件并再次读取它们,但我收到错误

[英]I'm trying to simply append two different data types into my xml file and read them again, but I am getting an error

I'm using Winforms and I get the following error when I press the load button after having hit the save button:我正在使用 Winforms,在按下保存按钮后按下加载按钮时出现以下错误:

InvalidOperationException: was not expected. InvalidOperationException:不是预期的。

Here is my code:这是我的代码:

private void save_Click(object sender, EventArgs e)
{
    // if you don't specify it, then it saves in the bin folder of the program
    FileStream fs = new FileStream(@"C:\Users\Owner\Desktop\Student2.Xml", FileMode.Append, FileAccess.Write); //@"C:\Users\Owner\Desktop\Student.Xml"

    StudentClass sc = new StudentClass();

    sc.Name = textBox1.Text;
    sc.Class = int.Parse(textBox2.Text);

    ls.Add(sc);
       
    StudentSerializer.Serialize(fs, ls);
    DicSerializer.Serialize(fs,
                            dict.Select(kv => new DicItem() { id = kv.Key, value = kv.Value }).ToArray());

    fs.Close();
}

private void load_Click(object sender, EventArgs e)
{
    FileStream fs = new FileStream(@"C:\Users\Owner\Desktop\Student2.Xml", FileMode.Open, FileAccess.Read);

    ls = (List<StudentClass>)StudentSerializer.Deserialize(fs);

    var orgDict = ((DicItem[])DicSerializer.Deserialize(fs))
                     .ToDictionary(i => i.id, i => i.value);

    foreach (var item in orgDict) 
         Debug.WriteLine(item.Key + " and " + item.Value);

    fs.Close();
}

My dictionary already has two items in it.我的字典里已经有两个项目了。 And my student class has two items as well from the textboxes.我的学生 class 在文本框中也有两个项目。 Also note, my xml file gets wiped clean after that error.另请注意,我的 xml 文件在该错误后被擦除干净。 Any help is appreciated thanks.感谢任何帮助。

Also, this is what my xml looks like right after hitting the save button:此外,这是我的 xml 在点击保存按钮后的样子:

<?xml version="1.0"?>
<ArrayOfStudentClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <StudentClass>
    <Name>wewewe</Name>
    <Class>2222</Class>
  </StudentClass>
</ArrayOfStudentClass><?xml version="1.0"?>
<DicItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <DicItem id="1" value="one" />
  <DicItem id="2" value="two" />
</DicItem>

The complaint is because of <?xml version="1.0"?> right before the first DictItem tag.投诉是因为<?xml version="1.0"?>就在第一个 DictItem 标记之前。 Such tag AFAIK should appear once only in the XML file.此类标签 AFAIK 应仅在 XML 文件中出现一次。

You should post the Deserializer code for each class for more investigation.您应该发布每个 class 的解串器代码以进行更多调查。

If you remove the second declaration than following code will work如果您删除第二个声明,则以下代码将起作用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;

            XmlReader reader = XmlReader.Create(FILENAME, settings);

            reader.ReadToFollowing("ArrayOfStudentClass");
            XElement ArrayOfStudentClass = (XElement)XElement.ReadFrom(reader);

            reader.ReadToFollowing("DicItem");
            XElement DicItem = (XElement)XElement.ReadFrom(reader);

        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试从SQL Server Compact数据库中读取数据,但是我一直收到相同的错误 - I'm trying to read data from my SQL Server Compact database, but I keep getting the same error 为什么在尝试从XML文件读取数据时出现Null Referenfe异常? - Why am I getting Null Referenfe exception while trying to read data from XML file? 我正在尝试从 xml 文件中读取正则表达式,但是当我将正则表达式传递给 C# 代码时,却得到了错误匹配 - I'm trying to read a Regex from an xml file but when i pass on the Regex to the C# code but I'm getting false matches 我正在尝试循环遍历文本文件中的数据,并将它的 append 循环到列表视图 - I am trying to loop through my data in text file and append it to list view 反序列化我的XML时,出现错误消息XML无效 - When deserializing my XML, I'm getting an error that XML is invalid 我在尝试XML序列化对象时遇到错误 - I am getting an error when trying to XML serialize an object 我在尝试将 CSV 文件读入列表时遇到错误<int> C#</int> - I am getting an error trying to read a CSV file in to a List<int> c# 我正在尝试获取过程信息并遇到两个错误,我该如何解决它们? - I'm trying to get process info and getting two errors how can i solve them? 当我尝试与我的数据库建立连接时出现错误 - Getting an error when I am trying to make a connection with my database 尝试在运行时创建动态对象列表时,为什么会出现编译错误? - Why am I getting a compile error when I'm trying to create a list of a dynamic object at runtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM