在vb.net中创建了一个从数据库中读取记录的web服务。正在尝试转换使用读取的选定记录 ExecuteReader
转换为xml。
<WebMethod()>
Public Function GetRoles(ByVal ID As String)
..........
With sqlcmd2
.Connection = sqlconn2
.CommandText = sqlquery2
.CommandType = CommandType.Text
.Parameters.AddWithValue("@ID", ID)
End With
sqlcmd2.CommandText = sqlquery2
Dim officers As New XElement("GetAnOfficerRoles")
Dim sqlreader2 = sqlcmd2.ExecuteReader()
If sqlreader2 IsNot Nothing And sqlreader2.HasRows Then
While sqlreader2.Read()
Dim officer As New XElement("Officer")
officers.Add(officer)
officer.Add(New XElement("ID_LOGIN",sqlreader2("ID").ToString))
officer.Add(New XElement("USER", sqlreader2("ID_USER").ToString))
officer.Add(New XElement("ROLES", sqlreader2("ROLES").ToString))
End While
End If
Dim settings As New XmlWriterSettings
settings.Indent = True
Dim mStream As New MemoryStream()
Dim writer As XmlWriter = XmlWriter.Create(mStream, settings)
officers.WriteTo(writer)
writer.Flush()
mStream.Position = 0
Dim sReader As New StreamReader(mStream)
Dim response As String = sReader.ReadToEnd()
Return response
我得到的答复都是一句话。。
<anyType d1p1:type="q1:string"><?xml version="1.0" encoding="utf-8"?>
<GetAnOfficerRoles><Officer><ID>abcde</ID><USER>2</USER><ROLES> 1, 5, 9</ROLES></Officer></GetAnOfficerRoles></anyType>
我需要的是xml
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 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>
<GetAnOfficerRolesResponse xmlns="http://tempuri.org/">
<GetAnOfficerRoles>
<Officer>
<ID>abcded</ID>
<USER>2</USER>
<ROLES> 1, 5, 9</ROLES>
</Officer>
</GetAnOfficerRoles>
</soap:Body>
</soap:Envelope>
非常感谢您的帮助。
我在其他项目中看到,它们使用了类和xmlserializer(gettype(classname))
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=false)> _
Partial Public Class ClassName
2条答案
按热度按时间9jyewag01#
使用xml linq创建元素:
9avjhtql2#
无需序列化类或多余代码,只需按如下方式更改getroles即可完成此操作:
然后从客户端可以详细说明作为xml接收的字符串。