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 8617491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:52:20+00:00 2026-06-12T05:52:20+00:00

I’m trying to use linq to find MyNode where the name equals Foo, and

  • 0

I’m trying to use linq to find MyNode where the name equals Foo, and make a copy of that node and add it to the XML but the new node should have name Bar, and then save the file.

<?xml version="1.0" encoding="utf-8"?>
<MyRoot>
  <MyNode Name="Foo">
    <Data Type="String">ABC</Data>
  </MyNode>
</MyRoot>

this code finds the node

    Dim doc As XDocument = XDocument.Load(xmlFile)
    Dim sheet = From item In doc...<MyRoot>...<MyNode> Select item Where item.@Name = "Foo"

and I’m trying to use this to add the node and change the name.

    sheet.@Name = "Bar" 'After excecuting this, sheet becomes "Nothing"
    doc.Root.Add(sheet.First)
    doc.Save(outFile)

however, after the setting sheet.@Name to “Bar”, then sheet becomes Nothing. If I comment out that line, the output will have two nodes, both named Bar. I suspect I am not doing this the “right way”, in either of terms of changing the attribute or adding this to the XDocument (or both)

  • 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-12T05:52:21+00:00Added an answer on June 12, 2026 at 5:52 am

    c# version (will try to translate)

    var doc = XDocument.Load(path);
                var node = doc.Elements("MyRoot").Elements("MyNode").FirstOrDefault(m => m.Attribute("Name").Value == "Foo");
                var newNode = new XElement(node);
                newNode.SetAttributeValue("Name", "Bar");
                doc.Root.Add(newNode);
    

    should be in vb

    Dim doc as XDocument = XDocument.Load(xmlFile)
    Dim sheet = From item...<MyNode>.FirstOrDefault(Function(m) m.@Name = "Foo")
    Dim newSheet As New XElement(sheet)
    newSheet.SetAttributeValue("Name", "Bar")
    doc.Root.Add(newSheet)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.