我想问一个问题,以了解如何将xml反序列化为对象来自api。
<Sistemrent>
<Sube id="1">
<Sube_Kodu>ABC</Sube_Kodu>
<Sube_Ismi>AAA BBB</Sube_Ismi>
<Kayit_ID>1</Kayit_ID>
</Sube>
<Sube id="2">
<Sube_Kodu>BCD</Sube_Kodu>
<Sube_Ismi>BBB CCC</Sube_Ismi>
<Kayit_ID>1</Kayit_ID>
</Sube>
</Sistemrent>
您可以找到我们为解析该xml而生成的以下类。
[XmlRoot(ElementName = "Sistemrent")]
public class Sistemrent
{
[XmlElement(ElementName = "Sube")]
public List<Sube> Sube { get; set; }
}
[XmlRoot(ElementName = "Sube")]
public class Sube
{
[XmlElement(ElementName = "Sube_Kodu")]
public string Sube_Kodu { get; set; }
[XmlElement(ElementName = "Sube_Ismi")]
public string Sube_Ismi { get; set; }
[XmlElement(ElementName = "Kayit_ID")]
public string Kayit_ID { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
它不会将xml转换为对象,需要您的帮助。
由于
答案 0 :(得分:0)
有效。请参阅以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Oppgave3Lesson1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Sistemrent));
Sistemrent sistemrent = (Sistemrent)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "Sistemrent")]
public class Sistemrent
{
[XmlElement(ElementName = "Sube")]
public List<Sube> Sube { get; set; }
}
[XmlRoot(ElementName = "Sube")]
public class Sube
{
[XmlElement(ElementName = "Sube_Kodu")]
public string Sube_Kodu { get; set; }
[XmlElement(ElementName = "Sube_Ismi")]
public string Sube_Ismi { get; set; }
[XmlElement(ElementName = "Kayit_ID")]
public string Kayit_ID { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
}