У меня есть следующий класс:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Super
{
[XmlRoot(ElementName = "V")]
public class V
{
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
[XmlAttribute(AttributeName = "value2")]
public string Value2 { get; set; }
}
[XmlRoot(ElementName = "L")]
public class L
{
[XmlAttribute(AttributeName = "item")]
public string item { get; set; }
[XmlAttribute(AttributeName = "upc")]
public string Upc { get; set; }
[XmlAttribute(AttributeName = "decimals")]
public string decimals { get; set; }
[XmlAttribute(AttributeName = "prod")]
public string Prod { get; set; }
[XmlAttribute(AttributeName = "Cclass")]
public string Cclass { get; set; }
}
[XmlRoot(ElementName = "P_B")]
public class P_B
{
[XmlAttribute(AttributeName = "pb")]
public string pb { get; set; }
[XmlAttribute(AttributeName = "udf_value")]
public string Udf_value { get; set; }
}
[XmlRoot(ElementName = "W")]
public class W
{
[XmlElement(ElementName = "P_B")]
public List<P_B> P_B { get; set; }
}
[XmlRoot(ElementName = "P_PRICE")]
public class P_PRICE
{
[XmlAttribute(AttributeName = "lvl")]
public string lvl { get; set; }
[XmlAttribute(AttributeName = "price")]
public string Price { get; set; }
[XmlAttribute(AttributeName = "A_req")]
public string A_req { get; set; }
[XmlAttribute(AttributeName = "season")]
public string Season { get; set; }
[XmlAttribute(AttributeName = "season")]
public string season { get; set; }
}
[XmlRoot(ElementName = "P_R")]
public class P_R
{
[XmlElement(ElementName = "P_PRICE")]
public P_PRICE P_PRICE { get; set; }
}
[XmlRoot(ElementName = "P_A")]
public class P_A
{
[XmlAttribute(AttributeName = "store")]
public string Store { get; set; }
[XmlAttribute(AttributeName = "A")]
public string A { get; set; }
[XmlAttribute(AttributeName = "m")]
public string M { get; set; }
[XmlAttribute(AttributeName = "ma")]
public string Ma { get; set; }
[XmlAttribute(AttributeName = "transfer")]
public string Transfer { get; set; }
[XmlAttribute(AttributeName = "Oout")]
public string Oout { get; set; }
}
[XmlRoot(ElementName = "P_AS")]
public class P_AS
{
[XmlElement(ElementName = "P_A")]
public List<P_A> P_A { get; set; }
}
[XmlRoot(ElementName = "P_T")]
public class P_T
{
[XmlAttribute(AttributeName = "came_from")]
public string Came_from { get; set; }
}
[XmlRoot(ElementName = "P")]
public class P
{
[XmlElement(ElementName = "W")]
public W W { get; set; }
[XmlElement(ElementName = "P_YY")]
public string P_YY { get; set; }
[XmlElement(ElementName = "P_R")]
public P_R P_R { get; set; }
[XmlElement(ElementName = "P_AS")]
public P_AS P_AS { get; set; }
[XmlElement(ElementName = "P_T")]
public P_T P_T { get; set; }
[XmlElement(ElementName = "D")]
public string D { get; set; }
[XmlElement(ElementName = "P_G")]
public string P_G { get; set; }
[XmlAttribute(AttributeName = "no")]
public string no { get; set; }
}
[XmlRoot(ElementName = "XY")]
public class XY
{
[XmlElement(ElementName = "V")]
public V V { get; set; }
[XmlElement(ElementName = "L")]
public L L { get; set; }
[XmlElement(ElementName = "P")]
public P P { get; set; }
}
[XmlRoot(ElementName = "ITEMS")]
public class ITEMS
{
[XmlElement(ElementName = "XY")]
public List<XY> XY { get; set; }
}
[XmlRoot(ElementName = "SETUP")]
public class SETUP
{
[XmlElement(ElementName = "ITEMS")]
public ITEMS ITEMS { get; set; }
}
}
Я пытаюсь сериализовать эти данные с:
SETUP f = new SETUP();
f.ITEMS.XY = new List<XY>();
using(var stream = new FileStream(Directory.GetCurrentDirectory() + "\\file.xml", FileMode.Create))
{
XmlSerializer X = new XmlSerializer(typeof(SETUP));
X.Serialize(stream, X);
}
Однако это дает ошибку, я пробовал другие варианты, однаковсе еще безуспешно.
Я гуглил и видел несколько сообщений stackoverflow, но я не могу заставить это записать файл с тем, что я даю.
Ошибка, которую я получаю:
Невозможно привести объект типа 'System.Xml.Serialization.XmlSerializer' к типу 'tool.SETUP'.
Как указано, я должен был использовать "f "не XI изменил его так сильно, что я потерял это из виду, однако теперь, когда я пытаюсь сериализоваться, я получаю:
SETUP f = new SETUP(); ITEMS x = new ITEMS(); List<XY> u = new List<XY>(); u.Add(new XY() { });
Неправильное использование Я получаю ошибку, в основном я не могу установитьзначения ... я собираюсь основываться на примере на stackoverflow -
"Имеет несколько недопустимых аргументов" - точная ошибка
Если я упрощаю классы LOT, то это работает, если есть тольконесколько элементов, но так как есть вложенные элементы, это может быть причиной проблемы?Разве не возможно создать экземпляр этого уровня xml?