Код: Выделить всё
Код: Выделить всё
public static class Namespaces
{
public const string tst = "myGivenNamespace";
}
[XmlRoot("myRootElement", Namespace = Namespaces.tst)]
public class MyRootElement
{
[XmlArray("myList"), XmlArrayItem(typeof(MyListItem), Namespace = Namespaces.tst)]
public List? MyList { get; set; }
}
[XmlRoot("myListItem", Namespace = Namespaces.tst)]
public class MyListItem
{
[XmlElement("myElementName", Namespace = Namespaces.tst)]
public MyElementName? ElementName { get; set; }
}
[XmlRoot("myElementName", Namespace = Namespaces.tst)]
public class MyElementName
{
[XmlAttribute("value", Namespace = Namespaces.tst)]
public string? Value { get; set; }
}
Код: Выделить всё
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("tst", "myGivenNamespace");
var serializer = new XmlSerializer(typeof(MyRootElement));
var root1 = new MyRootElement();
root1.MyList = new List();
root1.MyList.Add(new MyListItem { ElementName = new MyElementName { Value = "element1" } });
root1.MyList.Add(new MyListItem { ElementName = new MyElementName { Value = "element2" } });
// serialization
using (var fstream = new FileStream(@"c:\temp\serializertestOut.xml", FileMode.Create))
{
serializer.Serialize(fstream, root1, namespaces);
}
// deserialization
MyRootElement? root2 = null;
using (var fstream = new FileStream(@"c:\temp\serializertestIn.xml", FileMode.Open))
{
root2 = (MyRootElement?)serializer.Deserialize(fstream);
}
MyRootElement? root3 = null;
using (var fstream = new FileStream(@"c:\temp\serializertestOut.xml", FileMode.Open))
{
root3 = (MyRootElement?)serializer.Deserialize(fstream);
}
- root1 сериализуется в это:
Код: Выделить всё
- root2 создается как MyRootElement-Instance с пустым списком MyListElements.
- root3 создается правильно, как и ожидалось, поскольку он получает ранее созданный XML из сериализатора в качестве входных данных.
Подробнее здесь: https://stackoverflow.com/questions/798 ... prefix-bug
Мобильная версия