关于写xml字符串的一些积累
来源:互联网 发布:js页面自动跳转方法 编辑:程序博客网 时间:2024/05/21 14:46
最近在项目中用到用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格式的,最好运行一下的出来的才是正确的结果! 汗
0 0
- 关于写xml字符串的一些积累
- 关于Oracle的一些积累
- 关于多线程的一些积累
- 刚开始写 android 时的一些积累
- 新开张,写一些关于.net的东西!包括xml
- [积累]罗列关于Thread的一些知识点
- 关于规则引擎一些基本理论的积累
- 关于MediaPlayer使用的一些积累
- android 积累一些关于rxjava的知识点
- 关于struts标签库的一些积累
- JS自写的一些非法校验非法积累
- 写一些关于自己的
- 关于 XML 的一些基础知识
- 关于XML的一些资料
- 关于 XML 的一些基础知识
- 关于XML的一些基础知识
- 关于xml的一些见解
- 一些有用的关于程序的网址积累
- JdGrid树形表格分页
- UIImage的scale
- PHP Callback 回调类型
- Invalid location of tag
- Linux压缩和解压缩文件
- 关于写xml字符串的一些积累
- SpringMVC的注解命名方式
- 【Android基础】常用的布局和单位的简介
- Java中的s=s+5和s+=5的区别
- Servlet概念理解
- hdu1789(贪心)
- java中一个接口可以继承几个接口
- 使用runOnUiThread解决Handler无法调用含用子线程方法的问题
- 山东省第四届ACM省赛题——Contest Print Server(模拟)