将 XML 数据从 ASP.NET SOAP 服务检索到 ASP.NET aspx 页面
Retrieve XML data from ASP.NET SOAP Service to ASP.NET aspx page
我有网络服务,当用户输入 Id 时,return XML 数据。如果 ID 存在于数据库中,它将 return XML 数据,现在我创建 Test Project
作为参考使用此 SOAP Services
.
[WebMethod]
public DataTable GetAkontasById(int Id)
{
OracleConnection conn = new OracleConnection("DATA SOURCE=test-1:1521/test;USER ID=test;PASSWORD=test");
OracleDataAdapter dr = new OracleDataAdapter("Select * from AKONTAS where A_KONTO= '" + Id + "'", conn);
DataSet ds = new DataSet();
ds.Tables.Add("AKONTAS");
dr.Fill(ds, "AKONTAS");
DataTable tt = ds.Tables[0];
return tt;
}
这就是我的 xml 在 运行 Web Service
之后的样子
<DataTable xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="AKONTAS" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="AKONTAS" msprop:BaseTable.0="AKONTAS">
<xs:complexType>
<xs:sequence>
<xs:element name="A_KONTO" msprop:BaseColumn="A_KONTO" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="NAZIV" msprop:BaseColumn="NAZIV" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="SIFRA_RAD" msprop:BaseColumn="SIFRA_RAD" msprop:OraDbType="104" type="xs:string" minOccurs="0"/>
<xs:element name="OPSTINA" msprop:BaseColumn="OPSTINA" msprop:OraDbType="104" type="xs:string" minOccurs="0"/>
<xs:element name="MB" msprop:BaseColumn="MB" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="ULICA" msprop:BaseColumn="ULICA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="BROJ" msprop:BaseColumn="BROJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="PBROJ" msprop:BaseColumn="PBROJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="MJESTO" msprop:BaseColumn="MJESTO" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="PORESKI" msprop:BaseColumn="PORESKI" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="TRANSAKCIJ" msprop:BaseColumn="TRANSAKCIJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="INTERNET" msprop:BaseColumn="INTERNET" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="EMAIL" msprop:BaseColumn="EMAIL" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="KONTAKT" msprop:BaseColumn="KONTAKT" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="TELEFON" msprop:BaseColumn="TELEFON" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="FAKS" msprop:BaseColumn="FAKS" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="DAT_UNOS" msprop:BaseColumn="DAT_UNOS" msprop:OraDbType="106" type="xs:dateTime" minOccurs="0"/>
<xs:element name="PDVMB" msprop:BaseColumn="PDVMB" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="VRSTA_KLIJENTA" msprop:BaseColumn="VRSTA_KLIJENTA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="DRZAVA" msprop:BaseColumn="DRZAVA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<AKONTAS diffgr:id="AKONTAS1" msdata:rowOrder="0">
<A_KONTO>515534</A_KONTO>
<NAZIV>ABC</NAZIV>
<SIFRA_RAD>30017</SIFRA_RAD>
<MB>123456789000</MB>
<DAT_UNOS>2005-12-07T00:00:00+01:00</DAT_UNOS>
</AKONTAS>
</NewDataSet>
</diffgr:diffgram>
</DataTable>
现在 O 创建 aspx
页面,其中包含作为参考 Web Service.
这个 aspx
还需要 return XML 数据,现在我卡在这里,不知道如何获取这个 XML 数据并将其传递给 aspx
页。
你建议我做什么?
protected void btnProjeri_Click(object sender, EventArgs e)
{
AkontasService.AkontasSoapClient client =
new AkontasService.AkontasSoapClient();
var rezultat = client.GetAkontasById(Convert.ToInt32(txtAkonto.Text));
txtRezultat.Text = rezultat.ToString();
}
尝试以下 linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication132
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
List<XElement> AKONTAS = doc.Descendants("AKONTAS").ToList();
var results = AKONTAS.Select(x => x.Elements().Select(y => new KeyValuePair<string, string>(y.Name.LocalName, (string)y)).ToList()).ToList();
foreach (var result in results)
{
Console.WriteLine(string.Join(",", result.Select(x => x.Key + " : " + x.Value)));
}
Console.ReadLine();
}
}
}
我有网络服务,当用户输入 Id 时,return XML 数据。如果 ID 存在于数据库中,它将 return XML 数据,现在我创建 Test Project
作为参考使用此 SOAP Services
.
[WebMethod]
public DataTable GetAkontasById(int Id)
{
OracleConnection conn = new OracleConnection("DATA SOURCE=test-1:1521/test;USER ID=test;PASSWORD=test");
OracleDataAdapter dr = new OracleDataAdapter("Select * from AKONTAS where A_KONTO= '" + Id + "'", conn);
DataSet ds = new DataSet();
ds.Tables.Add("AKONTAS");
dr.Fill(ds, "AKONTAS");
DataTable tt = ds.Tables[0];
return tt;
}
这就是我的 xml 在 运行 Web Service
<DataTable xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="AKONTAS" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="AKONTAS" msprop:BaseTable.0="AKONTAS">
<xs:complexType>
<xs:sequence>
<xs:element name="A_KONTO" msprop:BaseColumn="A_KONTO" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="NAZIV" msprop:BaseColumn="NAZIV" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="SIFRA_RAD" msprop:BaseColumn="SIFRA_RAD" msprop:OraDbType="104" type="xs:string" minOccurs="0"/>
<xs:element name="OPSTINA" msprop:BaseColumn="OPSTINA" msprop:OraDbType="104" type="xs:string" minOccurs="0"/>
<xs:element name="MB" msprop:BaseColumn="MB" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="ULICA" msprop:BaseColumn="ULICA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="BROJ" msprop:BaseColumn="BROJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="PBROJ" msprop:BaseColumn="PBROJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="MJESTO" msprop:BaseColumn="MJESTO" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="PORESKI" msprop:BaseColumn="PORESKI" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="TRANSAKCIJ" msprop:BaseColumn="TRANSAKCIJ" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="INTERNET" msprop:BaseColumn="INTERNET" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="EMAIL" msprop:BaseColumn="EMAIL" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="KONTAKT" msprop:BaseColumn="KONTAKT" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="TELEFON" msprop:BaseColumn="TELEFON" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="FAKS" msprop:BaseColumn="FAKS" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="DAT_UNOS" msprop:BaseColumn="DAT_UNOS" msprop:OraDbType="106" type="xs:dateTime" minOccurs="0"/>
<xs:element name="PDVMB" msprop:BaseColumn="PDVMB" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="VRSTA_KLIJENTA" msprop:BaseColumn="VRSTA_KLIJENTA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
<xs:element name="DRZAVA" msprop:BaseColumn="DRZAVA" msprop:OraDbType="126" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<AKONTAS diffgr:id="AKONTAS1" msdata:rowOrder="0">
<A_KONTO>515534</A_KONTO>
<NAZIV>ABC</NAZIV>
<SIFRA_RAD>30017</SIFRA_RAD>
<MB>123456789000</MB>
<DAT_UNOS>2005-12-07T00:00:00+01:00</DAT_UNOS>
</AKONTAS>
</NewDataSet>
</diffgr:diffgram>
</DataTable>
现在 O 创建 aspx
页面,其中包含作为参考 Web Service.
这个 aspx
还需要 return XML 数据,现在我卡在这里,不知道如何获取这个 XML 数据并将其传递给 aspx
页。
你建议我做什么?
protected void btnProjeri_Click(object sender, EventArgs e)
{
AkontasService.AkontasSoapClient client =
new AkontasService.AkontasSoapClient();
var rezultat = client.GetAkontasById(Convert.ToInt32(txtAkonto.Text));
txtRezultat.Text = rezultat.ToString();
}
尝试以下 linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication132
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
List<XElement> AKONTAS = doc.Descendants("AKONTAS").ToList();
var results = AKONTAS.Select(x => x.Elements().Select(y => new KeyValuePair<string, string>(y.Name.LocalName, (string)y)).ToList()).ToList();
foreach (var result in results)
{
Console.WriteLine(string.Join(",", result.Select(x => x.Key + " : " + x.Value)));
}
Console.ReadLine();
}
}
}