使用NET类编写SOAP协议调用Web服务.doc
文本预览下载声明
PAGE \* MERGEFORMAT 7
使用.NET类编写SOAP协议调用Web服务
简介:使用.NET类编写SOAP消息,SOAP消息中包含用户的用户帐号,用户密码和帐号ID。使用HttpWebRequest类发送SOAP请求,请求远程服务器上Web服务程序(客户帐户信息),并使用HttpWebResponse类获取服务响应。
知识点:
命名空间:System.Xml
创建XML文档的类:XmlTextWriter
创建 XmlTextWriter 对象,设置用Tab键缩进
代码示例:
XmlTextWriter BookWriter = new XmlTextWriter( @\catalog\books.xml, Encoding.UTF8);
BookWriter.Formatting = Formatting.Indented;
编写XML文档的根元素
使用WriteStartDocument()方法和WriteEndDocument()方法创建XML声明
使用WriteStartElement()方法和WriteEndElement()方法创建根元素
代码示例:
BookWriter.WriteStartDocument();
BookWriter.WriteStartElement(books);
// 其他元素
BookWriter.WriteEndElement();
BookWriter.WriteEndDocument();
输出:
?xml version=1.0 encoding=utf-8 ?books
!-- write other elements here --
/books
编写元素
使用WriteElementString()方法创建不包含子元素和属性的元素
代码示例:
BookWriter.WriteElementString(price, 19.95);
输出:
price19.95/price
使用WriteStartElement()和WriteEndElement() 方法创建含有下级子元素和属性的元素
代码示例:
BookWriter.WriteStartElement(book);
BookWriter.WriteElementString(price, 19.95);
BookWriter.WriteEndElement();
输出:
book
price19.95/price
/book
编写属性
代码示例:
BookWriter.WriteStartElement(book);BookWriter.WriteAttributeString(price, 19.95);BookWriter.WriteEndElement();
输出:
book price=19.95 /
编写带有命名空间的元素
使用WriteElementString()方法或 WriteStartElement()方法编写带命名空间的元素
代码示例:
BookWriter.WriteStartElement(hr, Name, http://hrweb);
BookWriter.WriteString(Nancy Davolio);
BookWriter.WriteEndElement();
输出:
hr:NameNancy Davolio/hr:Name
编写带有命名空间的属性
使用WriteAttributeString()方法为元素添加带命名空间的属性
public void WriteAttributeString (
string prefix,
string localName,
string ns,
string value
)
参数
prefix:属性的命名空间前缀。
localName:属性的本地名称。
ns:属性的命名空间 URI。
value:属性值。
此方法写出具有用户定义的命名空间前缀的属性,并将其与给定的命名空间进行关联。如果前缀为“xmlns”,则此方法也将此当做命名空间声明对待,并将声明的前缀与给定属性值中提供的命名空间 URI 进行关联。在这种情况下,ns 参数可以为空引用。
代码示例:
xtw.WriteStartElement(bookstore);
// Write the namespace declaration
xtw.WriteAttributeString( xmlns, bk, null, urn:samples);
xtw.WriteStartElement(book);
// Lookup the prefix and then write the ISBN attribut
显示全部