Я пытаюсь понять сериализацию XML/десериализован в C#. У меня есть XML -файл. Но когда я попытался десериализировать его, я не получаю значение объекта правильно. Отладчик останавливается на линии ниже без каких -либо ошибок. В моем коде чего -то не хватает? < /P>
XmlSerializer deserializer = new XmlSerializer(typeof(Person));
code:
public class Program
{
//SOAP, the Simple Object Access Protocol, is an XML format used for the
//exchange of structured
//information,
//here we serialize object using this Format
static void Main(string[] args)
{
//Person a = new Person("adam","us");
////create a stream to write to a file
//Stream ab = File.OpenWrite("person.xml");
//// create the XMLSerializer
//XmlSerializer serializer = new XmlSerializer(typeof(Person));
//// serialize the object
//serializer.Serialize(ab, a);
//ab.Close();
Stream inputStream = File.OpenRead("person.xml");
XmlSerializer deserializer = new XmlSerializer(typeof(Person));
Person DeSerializedPerson =(Person)deserializer.Deserialize(inputStream);
foreach(Person person in DeSerializedPerson)
{
Console.WriteLine(person.Name);
Console.WriteLine(person.City);
}
}
}
public class Person: IEnumerable
{
private string name;
private string city;
Person p;
public Person()
{
// do nothing
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public string City
{
get { return this.city; }
set { this.city = value; }
}
public Person(string intnamePram,string cityParam)
{
this.name = intnamePram;
this.city = cityParam;
}
//public IEnumerator GetEnumerator()
//{
// return p.GetEnumerator();
//}
//IEnumerator IEnumerable.GetEnumerator()
//{
// return ContactList.GetEnumerator();
//}
public IEnumerator GetEnumerator()
{
return this.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public void Add(Person value)
{
this.p = value;
}
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... put-result
XML Deserializer не дает ожидаемого результата выходного результата ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение