Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

save System.Xml.XmlDocument object into native xml sql field

Posted on 2009-05-03
2
Medium Priority
?
930 Views
Last Modified: 2013-11-10
Hello,

I have a code to create a xml structure using System.Xml.XmlDocument class.
I want to save this datas into a native xml sql field, but I don't find the way to achive it.
Can you help me in this issue. I know there is saveTo method to save to file in a specific path, but I want to save into database.

Thanks.
XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlelem;
            
 
            xmldoc = new XmlDocument();
            //let's add the XML declaration section
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);
            //let's add the root element
            xmlelem=xmldoc.CreateElement("","Sobre_B","");
            xmldoc.AppendChild(xmlelem);
            for (int sobre = 0; sobre < 3; sobre++) // 3 sobres
            {
                xmlelem = xmldoc.CreateElement("", "sobre_A", "");
                xmldoc.ChildNodes.Item(1).AppendChild(xmlelem);
                for (int pieza = 0; pieza < 4; pieza++) // 4 piezas (piezas)
                {
                    xmlelem = xmldoc.CreateElement("", "pieza", "");
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).AppendChild(xmlelem);
                    xmlelem = xmldoc.CreateElement("", "coddos", "");
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).AppendChild(xmlelem);
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).ChildNodes.Item(0).InnerText = ((pieza+1) * (sobre+1)).ToString("00000");
 
                    xmlelem = xmldoc.CreateElement("", "cousco", "");
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).AppendChild(xmlelem);
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).ChildNodes.Item(1).InnerText = ((pieza+1) * (sobre+1)).ToString("00000") + ".01";
 
                    xmlelem = xmldoc.CreateElement("", "e_Anverso", "");
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).AppendChild(xmlelem);
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).ChildNodes.Item(2).InnerText = "c:\\anverso.jpg";
 
                    xmlelem = xmldoc.CreateElement("", "e_Reverso", "");
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).AppendChild(xmlelem);
                    xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).ChildNodes.Item(3).InnerText = "c:\\reverso.jpg";
                }
            }

Open in new window

0
Comment
Question by:imusa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 750 total points
ID: 24295073
There isn't much to this - just save the xml as a string (xmldoc.outerXml) to the table.

With a very trivial table (just a string name and an xml xmlitem),  I added a page to the ClubSiteExtended starter kit (not important though, the same thing would work anywhere)

The main part of the code was
        SqlDataSource1.InsertParameters["xml"].DefaultValue = xmldoc.OuterXml;
        SqlDataSource1.Insert();


The .aspx
---------------------------
<%@ Page Title="" Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true"
    CodeFile="xmls.aspx.cs" Inherits="xmls" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:TextBox ID="name" runat="server" />
    <asp:Button ID="run" runat="server" Text="Save" OnClick="run_Click" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"
        InsertCommand="INSERT INTO xmlitems(name,xml) VALUES (@name, @xml)" SelectCommand="SELECT [name], [xml] FROM [xmlitems]"
        UpdateCommand="UPDATE xmlitems SET xml = @xml WHERE (name = @name)">
        <InsertParameters>
            <asp:ControlParameter ControlID="name" Name="name" PropertyName="Text" />
            <asp:Parameter Name="xml" />
        </InsertParameters>
        <UpdateParameters>
            <asp:ControlParameter ControlID="name" Name="name" PropertyName="Text" />
            <asp:Parameter Name="xml" />
        </UpdateParameters>
    </asp:SqlDataSource>
</asp:Content>
---------------------------
 
The code-behind:
---------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
 
public partial class xmls : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void run_Click(object sender, EventArgs e)
    {
        XmlDocument xmldoc;
        XmlNode xmlnode;
        XmlElement xmlelem;
 
 
        xmldoc = new XmlDocument();
        //let's add the XML declaration section
        xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
        xmldoc.AppendChild(xmlnode);
        //let's add the root element
        xmlelem = xmldoc.CreateElement("", "Sobre_B", "");
        xmldoc.AppendChild(xmlelem);
        for (int sobre = 0; sobre < 3; sobre++) // 3 sobres
        {
            xmlelem = xmldoc.CreateElement("", "sobre_A", "");
            xmldoc.ChildNodes.Item(1).AppendChild(xmlelem);
            for (int pieza = 0; pieza < 4; pieza++) // 4 piezas (piezas)
            {
                xmlelem = xmldoc.CreateElement("", "pieza", "");
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).AppendChild(xmlelem);
                xmlelem = xmldoc.CreateElement("", "coddos", "");
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).AppendChild(xmlelem);
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).ChildNodes.Item(0).InnerText = ((pieza + 1) * (sobre + 1)).ToString("00000");
 
                xmlelem = xmldoc.CreateElement("", "cousco", "");
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).AppendChild(xmlelem);
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).ChildNodes.Item(1).InnerText = ((pieza + 1) * (sobre + 1)).ToString("00000") + ".01";
 
                xmlelem = xmldoc.CreateElement("", "e_Anverso", "");
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).AppendChild(xmlelem);
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).ChildNodes.Item(2).InnerText = "c:\\anverso.jpg";
 
                xmlelem = xmldoc.CreateElement("", "e_Reverso", "");
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).AppendChild(xmlelem);
                xmldoc.ChildNodes.Item(1).ChildNodes.Item(sobre).ChildNodes.Item(pieza).ChildNodes.Item(3).InnerText = "c:\\reverso.jpg";
            }
        }
        SqlDataSource1.InsertParameters["xml"].DefaultValue = xmldoc.OuterXml;
        SqlDataSource1.Insert();
    }
}
---------------------------

Open in new window

0
 

Author Closing Comment

by:imusa
ID: 31577380
OuterXml method was I need.

Tanks,
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question