Введение
Используя следующий код, я хотел бы создать вывод XML, упомянутый ниже: Однако, когда я пытаюсь сделать это, используя следующий код ( dotnet fiddle ):
using System;
using System.Xml.Serialization;
using System.IO;
using System.Text;
public class Program
{
[XmlRoot(ElementName = "OtpAlgorithm", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public class OtpAlgorithm
{
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "DeviceId", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public class DeviceId
{
[XmlElement(ElementName = "Model", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public string Model { get; set; }
}
[XmlRoot(ElementName = "ClientInfo", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public class ClientInfo
{
[XmlElement(ElementName = "platform", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public string Platform { get; set; }
}
[XmlRoot(ElementName = "Extension", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public class Extension
{
[XmlElement(ElementName = "ClientInfo", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public ClientInfo ClientInfo { get; set; }
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "vip", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Vip { get; set; } = "http://www.verisign.com/2006/08/vipservice";
}
[XmlRoot(ElementName = "GetSharedSecret", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public class GetSharedSecretRequest
{
[XmlElement(ElementName = "TokenModel", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public string TokenModel { get; set; }
[XmlElement(ElementName = "OtpAlgorithm", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public OtpAlgorithm OtpAlgorithm { get; set; }
[XmlElement(ElementName = "DeviceId", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public DeviceId DeviceId { get; set; }
[XmlElement(ElementName = "Extension", Namespace = "http://www.verisign.com/2006/08/vipservice")]
public Extension Extension { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; } = "http://www.verisign.com/2006/08/vipservice";
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; } = "http://www.w3.org/2001/XMLSchema-instance";
}
public static void Main()
{
var test = new GetSharedSecretRequest()
{
TokenModel = "Test",
OtpAlgorithm = new OtpAlgorithm()
{
Type = "AlgorithmTest"
},
DeviceId = new DeviceId()
{
Model = "Test Model"
},
Extension = new Extension()
{
ClientInfo = new ClientInfo()
{
Platform = "Test Plaftorm"
},
Type = "Another Test Type"
}
};
var serialized = Serialize(test);
//Basically this will fix it, but is ofcourse not a very nice solution:
//serialized = serialized.Replace("vip:", "");
Console.WriteLine(serialized);
if (serialized.Contains("vip:") || !serialized.Contains("<Extension xsi:type=\"Another Test Type\" xmlns:vip=\"http://www.verisign.com/2006/08/vipservice\">"))
{
Console.WriteLine("Test failed :(");
}
else
{
Console.WriteLine("Succeeded :)");
}
}
public static string Serialize<T>(T obj)
{
var serializer = new XmlSerializer(typeof(T));
using (var memstream = new MemoryStream())
{
serializer.Serialize(memstream, obj);
return Encoding.UTF8.GetString(memstream.GetBuffer());
}
}
}
Вывод XML, который я хотел бы сгенерировать:
<?xml version="1.0"?>
<GetSharedSecret xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.verisign.com/2006/08/vipservice">
<TokenModel>Test</TokenModel>
<OtpAlgorithm type="AlgorithmTest" />
<DeviceId>
<Model>Test Model</Model>
</DeviceId>
<Extension xsi:type="Another Test Type" xmlns:vip="http://www.verisign.com/2006/08/vipservice">
<ClientInfo>
<platform>Test Plaftorm</platform>
</ClientInfo>
</Extension>
</GetSharedSecret>
Но вместо этого мой код генерирует это:
<?xml version="1.0"?>
<GetSharedSecret xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.verisign.com/2006/08/vipservice">
<TokenModel>Test</TokenModel>
<OtpAlgorithm type="AlgorithmTest" />
<DeviceId>
<Model>Test Model</Model>
</DeviceId>
<Extension xsi:type="Another Test Type" xmlns:vip="http://www.verisign.com/2006/08/vipservice">
<vip:ClientInfo>
<vip:platform>Test Plaftorm</vip:platform>
</vip:ClientInfo>
</Extension>
</GetSharedSecret>
Есть лиспособ удалить vip:
перед элементами внутри класса Extension?
Единственное, что я обнаружил, это удаление пространства имен атрибута VIP:
[XmlAttribute(AttributeName = "vip", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Vip { get; set; } = "http://www.verisign.com/2006/08/vipservice";
Пространство именУдалено:
[XmlAttribute(AttributeName = "vip")]
public string Vip { get; set; } = "http://www.verisign.com/2006/08/vipservice";
Однако это приводит к тому, что вывод XML больше не содержит пространства имен.Что тоже не так.
Вопрос
Кто-нибудь знает, как решить эту проблему?Я создал dotnet fiddle для устранения проблемы.