手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

C# 如何生成 SOAP格式文件

时间:2024-10-13 00:30:49

1、生成soap文件,实际就是生成XML文件,我们可以用XMLTextWriter来写,也可以用XML序列化来生成。用VS新建一个工程,然后在代码中定义一个xmlWritervar ms = new MemoryStream(1024);//分配空间XmlTextWriter Xmltr = new XmlTextWriter(ms, Encoding.UTF8);

C# 如何生成 SOAP格式文件

2、然后接下来就可以用XMLWriter向里面写入内容了Xmltr.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/"); Xmltr.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); Xmltr.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema"); Xmltr.WriteAttributeString("xmlns", "soap", null, "http://schemas.xmlsoap.org/soap/envelope/"); Xmltr.WriteStartElement("Header", "http://schemas.xmlsoap.org/soap/envelope/"); Xmltr.WriteStartElement(null, "AuthInfo", "http://xxxxx.com/"); Xmltr.WriteElementString("UserName", "my"); Xmltr.WriteElementString("PassWord", "ps"); Xmltr.WriteEndElement(); Xmltr.WriteEndElement(); Xmltr.WriteStartElement("Body", "http://schemas.xmlsoap.org/soap/envelope/"); Xmltr.WriteStartElement(null, "GetLevel", "http://xxxx.com/"); Xmltr.WriteElementString("level", "0"); Xmltr.WriteEndElement(); Xmltr.WriteEndElement(); Xmltr.WriteEndDocument();

C# 如何生成 SOAP格式文件

3、最后写入文件,得到文件内容,注意是要把内容写入到文件中还是写入到内存中。

C# 如何生成 SOAP格式文件

4、实际上就是xml的生成问题,如果参数内容比较多的话,建议用XML的序列化来完成。

© 手抄报圈