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

  • Home
  • SEARCH
  • 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 8001887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:12:44+00:00 2026-06-04T16:12:44+00:00

I have an xml Template that I use to store values into a db

  • 0

I have an xml Template that I use to store values into a db (via a web-service). I have seen examples of how to update an xml String using linq. For example…

<Contacts>
    <Contact>
        <FirstName>Petar</FirstName>
        <LastName>Petrovic</LastName>
        <Email>p.petar@someemail.com</Email>
        <Address>Pere Perica 10</Address>
        <ZipCode>1000</ZipCode>
        <City>Belgrade</City>
        <State>Serbia</State>
    </Contact>
</Contacts>

If this was an xml doc you wanted to update, You would simply do something

XElement xmlDoc = new XElement("Contacts", 
                  from c in db.Contacts
                  orderby c.ContactID
                  select new XElement("Contact",
                         new XElement("ContactID", c.ContactID),
                         new XElement("FirstName", c.FirstName),
                         new XElement("LastName", c.LastName)));
xmlDoc.Save(Server.MapPath(@"~/export.xml"));

Which is pretty cool. But I would need to update nodes that are essentially the same except for their attributes. For example…

<?xml version="1.0" encoding="utf-8"?>
<dataTemplateSpecification id="id1" name="name1">
  <description>
    <html>text</html>
  </description>
  <templates>
    <template>
      <elements>
        <element id="element0" name="PatientId" display="Patient ID" dataType="String"  visable="true" readOnly="false">
        </element>
        <element id="element1" name="EMPIID" display="EMPI ID" dataType="String" visable="true" readOnly="true">
        </element>
      </elements>
      <dataTypeSpecifications>
        <dataTypeSpecification id="" baseType="KeyValuePair">
          <dictionaryDefinition>
            <item key="-1" value="-SELECT-" />
            <item key="1" value="YES" />
            <item key="0" value="NO" />
          </dictionaryDefinition>
        </dataTypeSpecification>
      </dataTypeSpecifications>

You see, I have similar nodes that are differentiated by their attributes, namely, the name attributes… as well as the value attributes… How would I use linq to update that? I am thinking I would select a new Xelement using kind of xPath type stuff where i would select the element by the name, and then just set that value? But I am a little confused about how to do that. Any ideas?

  • 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-06-04T16:12:46+00:00Added an answer on June 4, 2026 at 4:12 pm

    As you suggested you can select an element (attribute) by name, and assign a new value.
    I have altered your first element with this.

            var xml = XElement.Load("XMLFile1.xml");
    
            var node = xml.Descendants("element").Where(e => e.Attribute("id").Value == "element0").FirstOrDefault();
            if (node != null)
            {
                node.Attribute("name").Value = "something else";
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml template document that I need to load into an XmlDocument.
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
I have an xml column ExportTemplate in a table that holds an xsl template
We have an XML Publisher template that uses a query (built in query manager)
I have a XML file template like this that comes from some IC chips,
I have an XML Template file. This file contains a header and some predefined
I have a xml template like this <User> <UserId></UserId> <UserName></UserName> <Roles> <Role></Role> <Roles></Role> </Roles>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have XML that looks like this: <ROW ref=0005631 type=04 line=1 value=Australia/> <ROW ref=0005631
I have a project (built from an AppFuse template) that requires Maven 2.2.1. So

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.