Answer the question
In order to leave comments, you need to log in
How to properly deserialize XML in C#?
Please help me, I am learning to work with XML format in c#. There were problems deserializing the file. With direct deserialization, an error is displayed (screen below )
code:
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace Test
{
public class obj
{
string ProjectName;
string MemberRole;
string MemberName;
public obj()
{
}
}
public class XmlToXml
{
public string path;
public XmlDocument CurDoc = new XmlDocument();
public XmlNode DocRoot;
public void PutIn(string path)
{
this.path = path;
CurDoc.Load(path);
DocRoot = CurDoc.DocumentElement;
}
public void Deserialization()
{
XmlSerializer Deserializator = new XmlSerializer(typeof(obj[]));
using (FileStream origin = new FileStream(path, FileMode.Open))
{
obj[] ObjArray = (obj[])Deserializator.Deserialize(origin);
}
}
}
class Program
{
static void Main(string[] args)
{
XmlToXml a = new XmlToXml();
a.PutIn(@"C:\Users\Даниил\source\repos\4-Задачи\TaskFromTheInterviewXml1.0Tests\TestXmlL1.xml");
a.Deserialization();
}
}
}
<projects>
<project name="xml">
<member role="developer" name="Fedya"/>
<member role="manager" name="Ivan"/>
<member role="manager" name="Fedya"/>
</project>
</projects>
Answer the question
In order to leave comments, you need to log in
It’s better to start with taking your object and serializing it to xml and comparing it with what is published ...
Well, or “lazy” ask VS to generate classes from xml ( Edit -> PasteSpecial) and then compare it with your class
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question