Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4109448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:47:53+00:00 2026-05-20T21:47:53+00:00

Hi All Is been while since I am trying to figuare out how to

  • 0

Hi All
Is been while since I am trying to figuare out how to update my xml Column in my SQLServer Table. Ok for clear understanding I want to increment each and every id by 1.
Ok this is my Tablename called Setting(SettingId int pk, Name nVarchar(100), XmlSetting xml)

//my data.xml

<setting>
<a id=1/>
<b id=2/>
<c id=22>
</setting>

My C# code for Incrementing this code is below

XmlDocument xdoc = new XmlDocument();


private void SetAttribute(System.Xml.XmlNode node)//this code is running in the memory
  {

     System.Xml.XmlElement element = node as System.Xml.XmlElement;

      if (element != null)
            {
                int attributeCount = element.Attributes.Count;

                for (int i = 0; i < attributeCount; i++)
                {
                    System.Xml.XmlAttribute attribute = element.Attributes[i];

       if (string.Compare(attribute.Name, "Id", System.StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        int value;
                        if (int.TryParse(attribute.Value, out value))
                        {
                            attribute.Value = (value + 1).ToString();
                        }
                        else
                        {
                            attribute.Value = "1";
                        }
                    }
                }
                int childNodeCount = element.ChildNodes.Count;
                for (int i = 0; i < childNodeCount; i++)
                {
                    SetAttribute(element.ChildNodes[i]);
                }
            }
        }
        public void EditXmlFile()
        {
            xdoc.Load(FILE_NAME);
            SetAttribute(xdoc.FirstChild);
            return;
        }

All what I am asking for is to update this code into my Database table Setting

private void btnUpdate_Click(object sender, EventArgs e)
{

SqlConnection cnn = new SqlConnection("connectionPath");

SqlCommand cmd=new SqlCommand("Update Setting set SettingXml=@Settingxml where settingId=5",cnn);
cmd.Parameters.AddWithValue("@SettingXml","")//this where I am stacked because I am failing
        try
        {
           cnn.Open();
           cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            lblmsg.Text = "Error updating Xml " + ex.Message;
        }

}

How can update I update this SettingXml column with the above function because I want to increment every id by 1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:47:54+00:00Added an answer on May 20, 2026 at 9:47 pm

    You have to pass the updated XML as a string to your SqlParameter, so something like this:

    SqlCommand cmd=new SqlCommand("Update Setting set SettingXml=@SettingXml where settingId=5",cnn);
    cmd.Parameters.Add(new SqlParameter("@SettingXml", SqlDbType.Xml)).Value = xdoc.OuterXml;
    

    You might find it easier using Linq to XML for updating your XML going forward – XmlDocument is not very easy to use.

    Also your current XML is not valid – let’s assume it looks like this:

    <setting>
      <a id="1"/>
      <b id="2"/>
      <c id="22"/>
    </setting>
    

    Then you can increment your id’s using Linq to XML like this:

    XDocument doc = XDocument.Load("test.xml");
    doc.Root
       .Elements()
       .ToList()
       .ForEach(node => node.Attribute("id").Value = (Convert.ToInt32(node.Attribute("id").Value) + 1).ToString());
    
    string xml = doc.ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All i have something i have been trying to do for a while and
I've been trying to fix this problem for a while and have exhausted all
I have been trying for a while now trying to figure out how to
I have been using CodeIgniter all these while, and have solid foundation in OOP
I've been drooling over Django all day while coding up an internal website in
HI All, I've been using the basic split for a while - where I
I have been stumped all day on this problem. Basically I want to check
I've been struggling all morning to try and work out how mod_rewrite works after
I admit it's been a while since I've worked with C++, but from what
I'm completely new to python (and it's been a while since I've coded much).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.