如何构建具有属性的强类型 XML POST 请求
How To Build Strongly Typed XML POST Request With Attributes
我必须提出 XML 请求。我想知道如何创建一个具有属性节点的 XML 请求。这是我在 Postman 中尝试过的工作请求示例。
<ItemDestination DestinationType="geocode" Longitude="0.1278" Latitude="51.5074" RadiusKm="100"/>
在我必须提出的 XML 请求中,节点 ItemDestination 有几个属性,例如 "lattitude" 和 "longitude." 这是工具 [=31] 的 class =]2C#把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>
这不是我想要的。那么如何编辑 class 将上面的请求变成下面的请求呢?
<ItemDestination DestinationType="geocode" Longitude="0.1278" Latitude="51.5074" RadiusKm="100"/>
序列化代码:
XmlSerializer serializer = new XmlSerializer(typeof(GTASearchRequest).ToString());
var xmlRequest = serializer.Serialize(request);
此代码:
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
中生成所需的字符串
我必须提出 XML 请求。我想知道如何创建一个具有属性节点的 XML 请求。这是我在 Postman 中尝试过的工作请求示例。
<ItemDestination DestinationType="geocode" Longitude="0.1278" Latitude="51.5074" RadiusKm="100"/>
在我必须提出的 XML 请求中,节点 ItemDestination 有几个属性,例如 "lattitude" 和 "longitude." 这是工具 [=31] 的 class =]2C#把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>
这不是我想要的。那么如何编辑 class 将上面的请求变成下面的请求呢?
<ItemDestination DestinationType="geocode" Longitude="0.1278" Latitude="51.5074" RadiusKm="100"/>
序列化代码:
XmlSerializer serializer = new XmlSerializer(typeof(GTASearchRequest).ToString());
var xmlRequest = serializer.Serialize(request);
此代码:
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