2013-10-30 70 views
0

我有一個從java項目生成的webservice(wsdl)。其輸出得到xml格式。像這樣如何從wsdl webservice的xml輸出中獲取id

<?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">和顯示「援助「和其他鍵作爲表頭? 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>

它顯示了這個錯誤: - 「有多個根元素」

回答

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 
       } 
      } 
     } 
    } 
+0

代替文本.xml文件我把xml放入一個字符串並傳遞給XDocument.Load()。但我得到錯誤 - 「路徑中的非法字符」。當我創建一個XML文件,並給它的路徑運行正常。但我怎麼能得到標題和值像一張桌子..?列表<>只有一列。 – Sharon

+0

使用XDocument.Parse(「string」)... – Aravind

0

我想你的XML,我得到了這樣的結果。

enter image description here

您需要創建新StringReader實例和XML傳遞給它。由於您的DataSet將包含密鑰和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; 
     } 
+0

如果有多個行那麼XML異常來到'有單排data.its多個根元素' – Sharon

+0

工作 – Sharon