最近在项目中用到用xml模拟post提交的问题,一开始着实蒙了不知道怎么写xml文件,根据自己的一些经验,总结下来一些常用的xml写法,有许多不好的地方还请帮我多多改正!现在上代码了
这部分是写头部部分的,写出来的结果是这样子的
</pre><pre class="csharp" name="code"> private static void WriteXmlHeader(XmlWriter xmlTextWriter)
{
xmlTextWriter.WriteStartElement("s", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
xmlTextWriter.WriteStartElement("s", "Body", null);
xmlTextWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
xmlTextWriter.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
xmlTextWriter.WriteStartElement("", "getTransportWayList", "http://service.hop.service.ws.hlt.com/");
xmlTextWriter.WriteStartElement("userToken", "");
xmlTextWriter.WriteValue("password");
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndElement();
}
private static void WriteElement(XmlTextWriter writer, string name, string value)
{
if (!string.IsNullOrWhiteSpace(value))
{
writer.WriteElementString(name, value);
}
}
这部分是写头部部分的,写出来的结果是这样子的
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><getTransportWayList xmlns="http://service.hop.service.ws.hlt.com/"><userToken xmlns="">password</userToken></getTransportWayList></s:Body></s:Envelope>
这些都是比较基本的,可以看到,写出来的xml是没有xml声明的,而xml文件开头处的声明,我们也是可以自己选择要不要的,有时候还是默认utf-16的,就比如这样
private static void WriteXmlTest()
{
using (StringWriter stringWriter = new StringWriter())
{
// XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter); //没有前面的utf-8
XmlWriter xmlTextWriter = XmlWriter.Create(stringWriter); //有xml前面的申明,默认utf-16
// xmlTextWriter.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); //自定义xml申明部分
xmlTextWriter.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
xmlTextWriter.WriteAttributeString("xmlns", "soapenc", null, "http://schemas.xmlsoap.org/soap/encoding/");
xmlTextWriter.WriteAttributeString("xmlns", "tns", null, "http://edpost.vekinerp.com/soap/ecpp_wsdl");
xmlTextWriter.WriteAttributeString("xmlns", "types", null, "http://edpost.vekinerp.com/soap/ecpp_wsdl/encodedTypes");
xmlTextWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
xmlTextWriter.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
xmlTextWriter.WriteStartElement("soap", "Body", null);
xmlTextWriter.WriteAttributeString("soap", "encodingStyle", null, "http://schemas.xmlsoap.org/soap/encoding/");
xmlTextWriter.WriteStartElement("addOrder", "");
WriteElementExt(xmlTextWriter, "token", "xsi", "type", "xsd:string", "324324234");
WriteElementExt(xmlTextWriter, "orderInfo", "", "href", "#id1");
WriteElementExt(xmlTextWriter, "orderGoodsInfo", "", "href", "#id2");
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteStartElement("tns", "Body", null);
xmlTextWriter.WriteAttributeString("", "id", null, "id1");
xmlTextWriter.WriteAttributeString("xsi", "type", null, "tns:orderInfo");
WriteElementExt(xmlTextWriter, "paypalid", "xsi", "type", "xsd:string", "211313");
WriteElementExt(xmlTextWriter, "street1", "xsi", "type", "xsd:string", "fdfsfd");
WriteElementExt(xmlTextWriter, "pay_note", "xsi", "type", "xsd:string", "备注"); //支付备注
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndElement();
xmlTextWriter.Flush();
xmlTextWriter.Close();
Console.WriteLine(stringWriter.ToString());
Console.Read();
}
}
/// <summary>
/// 写xml元素
/// </summary>
/// <param name="writer">xml写</param>
/// <param name="name">元素名字</param>
/// <param name="preFix">元素前缀</param>
/// <param name="ns">元素命名</param>
/// <param name="url">对应的空间</param>
/// <param name="value">对应值</param>
private static void WriteElementExt(XmlWriter writer, string name, string preFix, string ns, string url, string value = "")
{
writer.WriteStartElement(name);
writer.WriteAttributeString(preFix, ns, null, url);
if (!string.IsNullOrWhiteSpace(value))
writer.WriteNmToken(value);
writer.WriteEndElement();
}
这一段代码打印出来的结果是这样的
<?xml version="1.0" encoding="utf-16"?><soap:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://edpost.vekinerp.com/soap/ecpp_wsdl" xmlns:types="http://edpost.vekinerp.com/soap/ecpp_wsdl/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><addOrder><token xsi:type="xsd:string">324324234</token><orderInfo href="#id1" /><orderGoodsInfo href="#id2" /></addOrder><tns:Body id="id1" xsi:type="tns:orderInfo"><paypalid xsi:type="xsd:string">211313</paypalid><street1 xsi:type="xsd:string">fdfsfd</street1><pay_note xsi:type="xsd:string">备注</pay_note></tns:Body></soap:Body></soap:Envelope>
里面可能有很多还可以改进的地方,不过目前我还没有找到,现学现卖!希望能够帮助到你们,也是对自己知识的一个积累(哪天要用上的直接拿下来),后面的post提交,应该就容易了。
using (WebClient webClient = new WebClient())
{
string result = string.Empty;
webClient.Headers[HttpRequestHeader.ContentType] = "text/xml; charset=utf-8";
webClient.Headers["SOAPAction"] = "http://tempuri.org/GET_FREIGHT_WAY";
result = Encoding.UTF8.GetString(webClient.UploadData(LogisticsWayUrl, "POST", Encoding.UTF8.GetBytes(xmlData)));
if (!string.IsNullOrWhiteSpace(result))
{
XmlDocument document = new XmlDocument();
document.LoadXml(result);
XmlNamespaceManager nmarager = new XmlNamespaceManager(document.NameTable);
nmarager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nmarager.AddNamespace("a", "http://tempuri.org/");
XmlNode FreightWayInfoList;
XmlNode GET_FREIGHT_WAYResponse = document.SelectSingleNode("/soap:Envelope/soap:Body/a:GET_FREIGHT_WAYResponse", nmarager);
FreightWayInfoList = GET_FREIGHT_WAYResponse.FirstChild.FirstChild.FirstChild;
List<LogisticsChannel> channelList = new List<LogisticsChannel>();
foreach (XmlNode xnS in FreightWayInfoList.ChildNodes)
{
string value = xnS.SelectSingleNode("FreightWayId").InnerText;
string name = xnS.SelectSingleNode("FreightWayName_CN").InnerText;
channelList.Add(new LogisticsChannel(name, value));
}
return channelList.ToArray<LogisticsChannel>();
}
下面的那些是提交之后得到的xml页,从中提取数据,在这里推荐大家可以用Fiddler这个工具来抓包玩玩
那段result就是根据xml文档来读取到FreightWayId和FreightWayName_CN的。
刚刚看了一下,结果显示的那些代码贴上去是html格式的,最好运行一下的出来的才是正确的结果! 汗