将具有多个节点的XML解析为SQL的问题

时间:2018-10-16 08:24:17

标签: sql xml openxml

我遇到了名为“ siBikNetQuery”的节点以及“ inquiryHeader”中的下3个节点的问题。有一个问题:

“ XML解析错误0xc00ce553在XML文本”“附近的第0行发生。 消息6602,级别16,状态2,过程sp_xml_preparedocument,第1行[批处理开始第1行] 错误描述为“以下标签未关闭:SiBikNet,siBikNetQuery,creditInquiry,inquiryHeader,testDataMarker。”。 消息8179,第16级,州际5行,第42行 找不到句柄0的准备好的语句。 消息6607,级别16,状态3,过程sp_xml_removedocument,第1行[批处理开始第1行] sp_xml_removedocument:为参数编号1提供的值无效。“

我不知道这是怎么了。

public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
 Hashtable hTable = new Hashtable();
 ArrayList duplicateList = new ArrayList();
 //Add list of all the unique item value to hashtable, which stores 
 combination of key, value pair.
 //And add duplicate item value in arraylist.
  foreach (DataRow drow in dTable.Rows)
   {
    foreach (DataRow drow2 in dTable.Rows)
     {
      if (hTable.Contains(drow[colName]))
       {
        if (DateTime.TryParse(drow["date"].ToString(), out DateTime res))
         {
          if (Convert.ToDateTime(drow["date"])<Convert.ToDateTime(drow2["date"]))
           {
            duplicateList.Add(drow);
           }
           if (Convert.ToDateTime(drow["date"]) > Convert.ToDateTime(drow2["date"]))
           {
           duplicateList.Add(drow);
           }
         }
       }
     else
     hTable.Add(drow[colName], string.Empty);
   }
  }
  //Removing a list of duplicate items from datatable.
  foreach (DataRow dRow in duplicateList)
            dTable.Rows.Remove(dRow);
  //Datatable which contains unique records will be return as output.
  return dTable;
}

另一个问题是,当我从XML路径删除这3行时,错误消失了,但是我得到了NULL值(我认为特定属性的路径存在问题):

 DECLARE @Handle AS INT; -- The handle of the XML data, passed to sp_xml_preparedocument
    DECLARE @Xml AS NVARCHAR(1000); -- The XML document for this example


    SET @Xml = N'
    <SiBikNet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.ws.bik.pl/ws/ki/2v2/types">
     <BIK_REQUEST xmlns="">
            <consentDate>2018-08-04</consentDate>
            <citizenshipStatus>citizen</citizenshipStatus>
            <nationality>PL</nationality>
            <pesel>123</pesel>
            <documentType>idCard</documentType>
            <documentId>CCH12</documentId>
            <surname>xxx</surname>
            <forename>yyy</forename>
            <country>PL</country>
            <postcode>11111</postcode>
            <city>wqreewqw</city>
            <street>wetww</street>
            <houseNumber>23</houseNumber>
            <localNumber>32</localNumber>
            <numberOfParticipants>1</numberOfParticipants>
            <applicationCurrency>PLN</applicationCurrency>
            <creditAmount>2000</creditAmount>
            <clientRelationToApplication>mainBorrower</clientRelationToApplication>
             </BIK_REQUEST>
     <siBikNetQuery xmlns="">
          <creditInquiry>
            <inquiryHeader>      
              <subscriberId>57000002</subscriberId>
              <subscriberUnitId>57000002</subscriberUnitId>
              <testDataMarker>false</testDataMarker>
            </inquiryHeader>
           </creditInquiry>
       </siBikNetQuery>
    </SiBikNet>';


    EXEC sys.sp_xml_preparedocument @Handle OUTPUT , @Xml, N'<SiBikNet xmlns:t="https://www.ws.bik.pl/ws/ki/2v2/types"/>'; --Prepare a parsed document 

    SELECT *
    FROM
           OPENXML(@Handle,'/*[local-name()="SiBikNet"]', 2)
               WITH (   consentDate NVARCHAR(10) '/*[local-name()="BIK_REQUEST"]',
                        citizenshipStatus NVARCHAR(10) '/*[local-name()="BIK_REQUEST"]',
                        nationality  NVARCHAR(10) '/*[local-name()="BIK_REQUEST"]' ,
                        subscriberId NVARCHAR(10) '/*[local-name()="siBikNetQuery"]',
                        subscriberUnitId NVARCHAR(10) '/*[local-name()="siBikNetQuery"]',
                        testDataMarker  NVARCHAR(10) '/*[local-name()="siBikNetQuery"]'
                    );

    EXEC sys.sp_xml_removedocument @Handle; 

0 个答案:

没有答案