English 中文(简体)
采用C# [封闭式]作为储存程序参数通过xml文档的简单途径
原标题:Simplest way to pass an xml file as a store procedure parameter using c# [closed]

I m 寻找最简单的途径,将Xml文档作为使用C#的储存程序参数。 实际上,我需要建立一个Xml文档,以便把这一xml储存在地库数据库中。 在数据库储存程序中,其参数为xml。 我已经建立了一个Xml文件,称斜体为 be。

 StringWriter stringWriter = null;
 XmlTextWriter writer = null;
 try
 {
  stringWriter = new StringWriter(new StringBuilder());
  writer = new XmlTextWriter(stringWriter);
  writer.Formatting = Formatting.Indented;
  writer.WriteStartDocument();
  writer.WriteComment("Holiday Info");

  writer.WriteStartElement("holidayInfo", "");

  for (int groupIndex = 0; groupIndex < numberOfGroup; groupIndex++)
        {
            for (int religionIndex = 0; religionIndex < numberOfReligion; religionIndex++)
            {
                for (int jobStationIndex = 0; jobStationIndex < numberOfJobStation; jobStationIndex++)
                {
                    writer.WriteStartElement("HOLIDAY", ""); 
                    writer.WriteAttributeString("intGroupID", chkGroupList.Items[groupIndex].Value.ToString());
                    writer.WriteAttributeString("intJobTypeId", "0");
                    writer.WriteAttributeString("intJobStationID", chkReligionList.Items[religionIndex].Value.ToString());
                    writer.WriteAttributeString("intHolidayID", ddlHolidayName.SelectedValue.ToString());
                    writer.WriteAttributeString("dtePermitedDate", DateTime.Now.ToShortDateString());
                    writer.WriteAttributeString("intReligionId", chkReligionList.Items[religionIndex].Value.ToString());
                    writer.WriteAttributeString("dteFromDate", txtFromDate.Text);
                    writer.WriteAttributeString("dteToDate", txtToDate.Text);
                    writer.WriteEndElement();
                }
            }

        }
      stringWriter.GetStringBuilder().ToString();
      return will be what if i want to sent this file to the store procedure parameter?;
    }
    finally
    {
        if (writer != null) writer.Close();
        if (stringWriter != null) stringWriter.Close();
    }

如果想将这一档案作为xml参数送交储存程序,回归类型将是什么?

这是我的储存程序。

CREATE PROCEDURE [dbo].[sprHoliday_InsertHolidaysGroupPermission]
@intUserID INT =NULL,
@xmlHolidayPermissionDetails XML,
@insertStatus AS VARCHAR(200) OUT
AS
BEGIN
DECLARE @intEmployeeID int
IF (@intUserID IS NOT NULL)
BEGIN
    SELECT @intEmployeeID = intEmployeeID From dbo.tblUserInfo WHERE   tblUserInfo.intUserID = @intUserID       
END 

BEGIN TRANSACTION
BEGIN TRY
   INSERT INTO  dbo.tblEmployeeGroupPermissionHolidays
                (intGroupID,
                 intJobTypeId,
                 intJobStationID,
                 intHolidayID,
                 dtePermitedDate,
                 intReligionId,
                 dteFromDate,
                 dteToDate)
        (SELECT  xmlHolidayPermissionDetails.item.value( @intGroupID[1] ,  INT ),
                 xmlHolidayPermissionDetails.item.value( @intJobTypeId[1] ,  INT ),
                 xmlHolidayPermissionDetails.item.value( @intJobStationID[1] ,  INT ),
                 xmlHolidayPermissionDetails.item.value( @intHolidayID[1] ,  INT ),
                 xmlHolidayPermissionDetails.item.value( @dtePermitedDate[1] ,  DATE ),
                 xmlHolidayPermissionDetails.item.value( @intReligionId[1] ,  INT ),
                 xmlHolidayPermissionDetails.item.value( @dteFromDate[1] ,  DATE ),
                 xmlHolidayPermissionDetails.item.value( @dteToDate[1] ,  DATE )
          FROM   @xmlHolidayPermissionDetails.nodes( //HOLIDAY ) AS xmlHolidayPermissionDetails(item)
         )
   COMMIT 
   INSERT INTO dbo.tblHRDataHistory values( Insert , Holiday Permission Data Insert , tblEmployeeGroupPermissionHolidays ,GETDATE(),@intEmployeeID)
   SET @insertStatus =  Holiday Permission has been iserted succesfully  
 END TRY

 BEGIN CATCH
 Rollback
 SELECT @insertStatus =  There was an error!   + ERROR_MESSAGE()
 END CATCH 

 END
最佳回答
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(string);

这应当行之有效。

问题回答

整个文件:

XDocument doc = XDocument.Parse(text);

如果你刚才需要一个内容,你可以很容易地补充另一个文件:

XElement element = XElement.Parse(text);

假设你重新使用。 NET 3.5 - 拉脱维亚刑法和刑法准则与旧文本相比有很大的nic。

请注意,你一再表示,你需要“返回型号为xml”的功能。 该网络中没有“xml”类,但如XElement/code>、XDocument等类型是XML AP。 您不必另立一种方法,以回去,这些方法已经作为<条码>XDocument.Parse <>/code>和XElement。 如上所示,Parse

我怀疑你对自己的要求感到困惑——指出“返回一个xmlfile也是一个奇怪的问题。 是你试图write>。 档案数据? 从档案中读取? 如果没有的话,哪些文件与你的要求有关?

如能提供更多详情,详细说明您真的做了些许,我们可以为你们提供更多东西。

贵问题中的“简单”限定语非常主观,因为不同的方法可以合理有效。 然而,这里的“简单”解决办法是:

XDocument doc = XDocument.Parse(xmlstring);

XElement el = doc.Root;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...