我必须制作XML请求。我想知道如何创建一个具有属性节点的XML请求。以下是我在Postman中尝试过的工作请求示例。
<ItemDestination DestinationType="geocode" Longitude="0.1278" Latitude="51.5074" RadiusKm="100"/>
在我必须做的XML请求中,节点ItemDestination有几个属性,如“lattitude”和“longitude”。这是XML2C#工具将xml转换为的类:
[XmlRoot(ElementName = "ItemDestination")]
public class ItemDestination
{
[XmlAttribute(AttributeName = "DestinationType")]
public string DestinationType { get; set; }
[XmlAttribute(AttributeName = "Longitude")]
public string Longitude { get; set; }
[XmlAttribute(AttributeName = "Latitude")]
public string Latitude { get; set; }
[XmlAttribute(AttributeName = "RadiusKm")]
public string RadiusKm { get; set; }
}
现在当我拨打电话而不是具有我想要的属性的节点时,当我打开一个提琴手并查看请求时它看起来像这样:
<ItemDestination>
<DestinationType>geocode</DestinationType>
<Longitude>2.64663399999995</Longitude>
<Latitude>39.57119</Latitude>
<RadiusKm>5</RadiusKm>
</ItemDestination>
这不是我想要的。那么如何编辑类以将上述请求转换为以下请求?
<ItemDestination DestinationType="geocode" Longitude="0.1278" Latitude="51.5074" RadiusKm="100"/>
序列化代码:
XmlSerializer serializer = new XmlSerializer(typeof(GTASearchRequest).ToString());
var xmlRequest = serializer.Serialize(request);
答案 0 :(得分:0)
此代码:
var data = new ItemDestination
{
DestinationType = "geocode",
Longitude = "2.64663399999995",
Latitude = "39.57119",
RadiusKm = "5"
};
var serializer = new XmlSerializer( typeof( ItemDestination ) );
var writer = new StringWriter();
serializer.Serialize( writer, data );
writer.Flush();
var xmlRequest = writer.GetStringBuilder().ToString();
其中ItemDestination
被声明为:
[XmlRoot(ElementName = "ItemDestination")]
public class ItemDestination
{
[XmlAttribute(AttributeName = "DestinationType")]
public string DestinationType { get; set; }
[XmlAttribute(AttributeName = "Longitude")]
public string Longitude { get; set; }
[XmlAttribute(AttributeName = "Latitude")]
public string Latitude { get; set; }
[XmlAttribute(AttributeName = "RadiusKm")]
public string RadiusKm { get; set; }
}
在xmlRequest