Мне нужно отправить Xml веб-службы другой компании в указанном формате.
Они не могут предоставить XSD для автоматического создания классов с помощью утилиты, поэтому я описал все используемые классы и попробую использовать XmlSerializer.
Пример в требуемом формате (один из сотен типов):
<?xml version="1.0" encoding="utf-8"?>
<PurchasePlan>
<Guid description="Guid" type="string">72F0543E-B95A-4E5F-9F96-F8126F16C107</Guid>
<CreateDate description="Date and time of creation" type="DateTime">2018-12-20T10:10:15.000Z</CreateDate>
<RegistrationNumber description="RegistrationNumber of purchase plan" type="string"/>
<Name description="Purchase plan name" type="string">Purchase no.8 according to 223 Federal Law</Name>
<Status>
<Name description="Status name" type="string">Approved</Name>
<Code description="Status code" type="int">1</Code>
</Status>
....
....
</PurchasePlan>
Каждый узел в Xml может содержать значение (типы: int, string, guid, другие классы) и атрибуты (описание, тип, EntityName)
Я пытаюсь создать универсальный класс Etprf_Element, который будет сериализован с атрибутами и значением
public class Etprf_Element
{
[XmlAttribute]
public string description { get; set; }
[XmlAttribute]
public string type { get; set; }
[XmlAttribute]
public string EntityName { get; set; }
[XmlElement]
public object Value { get; set; }
}
Это класс для генерации PurchasePlan.Xml
[XmlRootAttribute("PurchasePlan", Namespace = "http://www.cpandl.com", IsNullable = true)]
public class Etprf_PurchasePlan
{
[XmlElement]
public Etprf_Element Guid = new Etprf_Element() {description = "Guid", type = "string" };
public Etprf_Element CreateDate = new Etprf_Element() { description = "Date and time of creation", type = "DateTime" };
public Etprf_Element RegistrationNumber = new Etprf_Element() { description = "RegistrationNumber of purchase plan", type = "string" }
public Etprf_Element Name = new Etprf_Element<String>() { description = "Purchase plan name", type = "string" };
....
}
Работа с классом и значениями заполнения:
Etprf_PurchasePlan oEtprf_PurchasePlan = new Etprf_PurchasePlan();
oEtprf_PurchasePlan.Guid.Value = Guid.NewGuid().ToString(); // FieldValue = Guid.NewGuid().ToString();
oEtprf_PurchasePlan.CreateDate.Value = GetPlanCreateDateTime().Date.ToString();
oEtprf_PurchasePlan.RegistrationNumber.Value = "";
oEtprf_PurchasePlan.Name.Value = GetPlanName();
...
Итак, я получаю
<?xml version="1.0" encoding="utf-8"?>
<PurchasePlan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cpandl.com">
<Guid description="Guid" type="string">
<Value>a24cf959-4944-452d-8eb9-2a8a8513a616</Value>
</Guid>
<CreateDate description="Date and time of creation" type="DateTime">
<Value>2018-12-20T10:10:15.000Z</Value>
</CreateDate>
<RegistrationNumber description="RegistrationNumber of purchase plan" type="string">
<Value/>
</RegistrationNumber>
Если я опишу класс как базовые типы
public class PurchasePlan
{
Guid Guid
DateTime CreateDate
string RegistrationNumber
...
...
}
он будет отлично сериализован без атрибутов.Но они мне нужны.