如何从wsdl webservice的xml输出中获取id

时间:2013-10-30 11:29:26

标签: c# asp.net xml web-services

我有一个从java项目生成的webservice(wsdl)。它的输出为xml格式。像这样的东西

<?xml version="1.0" encoding="UTF-8"?><list><map><entry key="aid">160608</entry><entry key="aDate">2013-10-24 00:00:00.0 IST</entry><entry key="insuranceType">Self Pay</entry><entry key="status">New</entry><entry key="pid">160576</entry><entry key="pChartno" /><entry key="lName">Mathur</entry><entry key="fName">Gaurav</entry><entry key="mName">mathur</entry><entry key="gender">Male</entry><entry key="ssn" /><entry key="providerId">2030</entry><entry key="providerFname">lakshman</entry></map></list>

在我的cs文件中,我解析xml并将结果放入数据集中。 这是我的代码

DataSet dsresult = new DataSet();
XmlDocument xml = new XmlDocument();
xml.LoadXml("--XML String From Webservice Here--");
XmlElement exelement = xml.DocumentElement;

XmlNodeReader nodereader = new XmlNodeReader(exelement);
dsresult.ReadXml(nodereader, XmlReadMode.Auto);
return dsresult;

现在我将数据集绑定到gridview,它只显示此

Image of grid view here

如何从每个节点获取密钥,即从<entry key="aid">并显示“aid”和其他键作为表头? cs代码会有所帮助。

我想要一个网格数据 this (Click here for grid image)

编辑1

对于单行数据,Sam的代码正在运行。但是当我输入像这样的xml字符串时

<?xml version="1.0" encoding="UTF-8"?><list><map><entry key="aid">173661</entry><entry key="aDate">2013-10-28 00:00:00.0 IST</entry><entry key="insuranceType">Self Pay</entry><entry key="status">Serviced</entry><entry key="pid">163686</entry><entry key="pChartno" /><entry key="lName">Bec&amp;&amp;kwith</entry><entry key="fName">Burt</entry><entry key="mName" /><entry key="gender">Male</entry><entry key="ssn" /><entry key="providerId">137935</entry><entry key="providerFname">test</entry></map><map><entry key="aid">173675</entry><entry key="aDate">2013-10-28 00:00:00.0 IST</entry><entry key="insuranceType">Self Pay</entry><entry key="status">New</entry><entry key="pid">2038</entry><entry key="pChartno" /><entry key="lName">Velusamy </entry><entry key="fName">Anand</entry><entry key="mName">M</entry><entry key="gender">Male</entry><entry key="ssn">12345690</entry><entry key="providerId">137935</entry><entry key="providerFname">test</entry></map><map><entry key="aid">173679</entry><entry key="aDate">2013-10-28 00:00:00.0 IST</entry><entry key="insuranceType">Self Pay</entry><entry key="status">Serviced</entry><entry key="pid">140417</entry><entry key="pChartno" /><entry key="lName">alex</entry><entry key="fName">pandian</entry><entry key="mName" /><entry key="gender">Male</entry><entry key="ssn" /><entry key="providerId">137935</entry><entry key="providerFname">test</entry></map></list>

它显示此错误: - “有多个根元素”

2 个答案:

答案 0 :(得分:0)

我尝试了你的XML,我得到了这个结果。

enter image description here

您需要创建StringReader的新实例并将XML传递给它。当DataSet包含key和desc列时,您需要创建一个DataTable来将行转换为列,因此需要额外的功能。

 protected void Page_Load(object sender, EventArgs e)
        {
            DataSet dsresult = new DataSet();
            XmlDocument xml = new XmlDocument();
            xml.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><list><map><entry key=\"aid\">160608</entry><entry key=\"aDate\">2013-10-24 00:00:00.0 IST</entry><entry key=\"insuranceType\">Self Pay</entry><entry key=\"status\">New</entry><entry key=\"pid\">160576</entry><entry key=\"pChartno\" /><entry key=\"lName\">Mathur</entry><entry key=\"fName\">Gaurav</entry><entry key=\"mName\">mathur</entry><entry key=\"gender\">Male</entry><entry key=\"ssn\" /><entry key=\"providerId\">2030</entry><entry key=\"providerFname\">lakshman</entry></map></list>");
            XmlElement exelement = xml.DocumentElement;

            dsresult.ReadXml(new XmlTextReader(new System.IO.StringReader(exelement.InnerXml)));

            DataTable transposedTable = GenerateTransposedTable(dsresult.Tables[0]);

            grd.DataSource = transposedTable;
            grd.DataBind();
        }

 private DataTable GenerateTransposedTable(DataTable inputTable)
        {
            DataTable outputTable = new DataTable();

            // Add columns by looping rows

            // Header row's first column is same as in inputTable
            outputTable.Columns.Add(inputTable.Columns[0].ColumnName.ToString());

            // Header row's second column onwards, 'inputTable's first column taken
            foreach (DataRow inRow in inputTable.Rows)
            {
                string newColName = inRow[0].ToString();
                outputTable.Columns.Add(newColName);
            }

            // Add rows by looping columns        
            for (int rCount = 1; rCount <= inputTable.Columns.Count - 1; rCount++)
            {
                DataRow newRow = outputTable.NewRow();

                // First column is inputTable's Header row's second column
                newRow[0] = inputTable.Columns[rCount].ColumnName.ToString();
                for (int cCount = 0; cCount <= inputTable.Rows.Count - 1; cCount++)
                {
                    string colValue = inputTable.Rows[cCount][rCount].ToString();
                    newRow[cCount + 1] = colValue;
                }
                outputTable.Rows.Add(newRow);
            }

            return outputTable;
        }

答案 1 :(得分:0)

试试这个

你的Xml

<?xml version="1.0" encoding="UTF-8" ?> 
<list>
<map>
 <entry key="aid">160608</entry> 
 <entry key="aDate">2013-10-24 00:00:00.0 IST</entry> 
 <entry key="insuranceType">Self Pay</entry> 
 <entry key="status">New</entry> 
<entry key="pid">160576</entry> 
<entry key="pChartno" /> 
<entry key="lName">Mathur</entry> 
<entry key="fName">Gaurav</entry> 
<entry key="mName">mathur</entry> 
<entry key="gender">Male</entry> 
<entry key="ssn" /> 
<entry key="providerId">2030</entry> 
<entry key="providerFname">lakshman</entry> 
</map>
</list>

private void LoadData()
    {
        XDocument xDoc = XDocument.Load("E:\\test.xml");
        XElement xEle = xDoc.XPathSelectElement("//map");
        List<string> lstValues = new  List<string>();
        if (xEle != null)
        {
            foreach (XElement xElement in xEle.Descendants())
            {
                if (xElement.Attribute("key") != null)
                {
                    lstValues.Add(xElement.Attribute("key").Value); // In this List you will get everything
                }
            }
        }
    }