xml 文件的反序列化在 visual studio 中以通常的方式进行时不起作用
Deserialisation of xml file not working when I do it the usual way in visual studio
我正在尝试反序列化 xml 文件。我过去曾这样做过,它总是很有效,但这次不是。如果有人看到我的错误并可以提供帮助,那将不胜感激!上次我是在 unity 中使用它,这次是 visual studio 控制台应用程序。
编辑: 在 visual studio 中工作时,我可能需要以某种方式设置 "xmlns" 属性,但是要设置什么?
我收到此错误消息:
Error in XML-Document (2,2)
<Information xmlns=''> not exspected
我的文件如下所示:
Information.xml:
<?xml version="1.0" encoding="utf-8"?>
<InformationenContainer>
<Informations>
<Information ID="foo"/>
</Informations>
</InformationenContainer>
Program.cs:
class Program
{
static void Main(string[] args)
{
string filename = "C:\Users\Ruben Bohnet\Documents\Visual Studio 2015\Projects\Tool\Tool\Informationen.xml";
InformationenContainer IC = InformationenContainer.Load(filename);
}
}
InformationController.cs:
using System.IO;
using System.Xml.Serialization;
[XmlRoot("InformationenContainer")]
public class InformationenContainer
{
[XmlArray("Informations"), XmlArrayItem("Information",typeof(Information))]
public Information[] Informations;
public static InformationenContainer Load(string path)
{
var serializer = new XmlSerializer(typeof(InformationenContainer));
using (var stream = new FileStream(path, FileMode.Open))
{
InformationenContainer ic = serializer.Deserialize(stream) as InformationenContainer;
return ic;
}
}
}
Information.cs:
using System;
using System.Xml.Serialization;
public class Information
{
//Attribuites to serialize
[XmlAttribute("ID")]
public String ID;
}
通过创建一个 InformationContainer 并对其进行序列化,我自己找到了解决方案,生成的 xml 文件如下所示:
<?xml version="1.0"?>
<InformationenContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Informations>
<Information ID="foo" />
</Informations>
</InformationenContainer>
我正在尝试反序列化 xml 文件。我过去曾这样做过,它总是很有效,但这次不是。如果有人看到我的错误并可以提供帮助,那将不胜感激!上次我是在 unity 中使用它,这次是 visual studio 控制台应用程序。
编辑: 在 visual studio 中工作时,我可能需要以某种方式设置 "xmlns" 属性,但是要设置什么?
我收到此错误消息:
Error in XML-Document (2,2)
<Information xmlns=''> not exspected
我的文件如下所示:
Information.xml:
<?xml version="1.0" encoding="utf-8"?>
<InformationenContainer>
<Informations>
<Information ID="foo"/>
</Informations>
</InformationenContainer>
Program.cs:
class Program
{
static void Main(string[] args)
{
string filename = "C:\Users\Ruben Bohnet\Documents\Visual Studio 2015\Projects\Tool\Tool\Informationen.xml";
InformationenContainer IC = InformationenContainer.Load(filename);
}
}
InformationController.cs:
using System.IO;
using System.Xml.Serialization;
[XmlRoot("InformationenContainer")]
public class InformationenContainer
{
[XmlArray("Informations"), XmlArrayItem("Information",typeof(Information))]
public Information[] Informations;
public static InformationenContainer Load(string path)
{
var serializer = new XmlSerializer(typeof(InformationenContainer));
using (var stream = new FileStream(path, FileMode.Open))
{
InformationenContainer ic = serializer.Deserialize(stream) as InformationenContainer;
return ic;
}
}
}
Information.cs:
using System;
using System.Xml.Serialization;
public class Information
{
//Attribuites to serialize
[XmlAttribute("ID")]
public String ID;
}
通过创建一个 InformationContainer 并对其进行序列化,我自己找到了解决方案,生成的 xml 文件如下所示:
<?xml version="1.0"?>
<InformationenContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Informations>
<Information ID="foo" />
</Informations>
</InformationenContainer>