asp.net,vb.net。目标是将XML数据复制到表(customerDetail)中,但是当我执行存储过程时,XML数据不会复制到表中,也不会出现任何错误。
我的xml文件:
<?xml version="1.0" standalone="yes"?>
<q:Customers xmlns="http://quakeml.org/xmlns/bed/1.2" xmlns:catalog="http://anss.org/xmlns/catalog/0.1" xmlns:q="http://quakeml.org/xmlns/quakeml/1.2">
<Customer Id ="1">
<Name>John Hammond</Name>
<Country>United States</Country>
</Customer>
<Customer Id = "2">
<Name>Mudassar Khan</Name>
<Country>India</Country>
</Customer>
<Customer Id ="3">
<Name>Suzanne Mathews</Name>
<Country>France</Country>
</Customer>
<Customer Id ="4">
<Name>Robert Schidner</Name>
<Country>Russia</Country>
</Customer>
</q:Customers>
我的存储过程:
ALTER PROCEDURE [dbo].[InsertXML]
@xml XML
AS
BEGIN
;WITH XMLNAMESPACES ('http://quakeml.org/xmlns/quakeml/1.2' as q)
INSERT INTO CustomerDetails
SELECT
Customer.value('@Id','INT') AS CustomerId, --ATTRIBUTE
Customer.value('(Name/text())[1]','VARCHAR(100)') AS Names, --TAG
Customer.value('(Country/text())[1]','VARCHAR(100)') AS Country --TAG
FROM
@xml.nodes('//q:Customers/Customer') as TEMPTABLE(Customer)
END
我在asp.net中的vb.net代码
Imports System.IO
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class UploadXml
Inherits System.Web.UI.Page
Protected Sub UploadXML(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_upload.Click
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim filePath As String = Server.MapPath("~/Uploads/") & fileName
FileUpload1.SaveAs(filePath)
Dim xml As String = File.ReadAllText(filePath)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("InsertXML")
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@xml", xml)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
End Class
答案 0 :(得分:0)
客户属于默认命名空间http://quakeml.org/xmlns/bed/1.2
。
试试这个:
WITH XMLNAMESPACES ('http://quakeml.org/xmlns/quakeml/1.2' as q,
default 'http://quakeml.org/xmlns/bed/1.2')
SELECT
Customer.value('@Id','INT') AS CustomerId, --ATTRIBUTE
Customer.value('(Name/text())[1]','VARCHAR(100)') AS Names, --TAG
Customer.value('(Country/text())[1]','VARCHAR(100)') AS Country --TAG
FROM
@xml.nodes('/q:Customers/Customer') as TEMPTABLE(Customer);