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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:24:11+00:00 2026-05-20T02:24:11+00:00

I have following xml structure I want to update it through c# How to

  • 0

I have following xml structure I want to update it through c#
How to update it
Is it possible to update through Linq if yes then how to do it?
I want to add UnitTest, TestList, TestEntry and UnitTestResults elements through code.

`

<?xml version="1.0" encoding="UTF-8"?>
<TestRun id="1" xmlns="http://microsoft.com/schemas">
     <TestDefinitions>
    <UnitTest name="Test1" id="T1">
      <Execution id="E1" />   
    </UnitTest>
    <UnitTest name="Test2" id="T2">
      <Execution id="E2" />   
    </UnitTest>
        :
        :

  </TestDefinitions>
    <TestLists>
    <TestList name="List1" id="L1" />
    <TestList name="List2" id="L2" />
     :
      :

  </TestLists>
  <TestEntries>
    <TestEntry testId="T1" executionId="E1" testListId="L1" />
    <TestEntry testId="T2" executionId="E2" testListId="L2" />
    :
    :
  </TestEntries>
  <Results>
    <UnitTestResult executionId="E1" testId="T1" testName="Test1" >
      <Output>
        <ErrorInfo>
          <Message>Hi</Message>
        </ErrorInfo>
      </Output>
    </UnitTestResult>
  </Results>
  <Results>
    :
    :
</TestRun>

`

  • 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-20T02:24:12+00:00Added an answer on May 20, 2026 at 2:24 am

    Yes, it is possible by LINQ. This is the example code to add new UnitTest element:

    XDocument doc = XDocument.Load( "tests.xml" );
    XNamespace ns = "http://microsoft.com/schemas";
    
    XElement unitTest = new XElement( ns + "UnitTest",
        new XElement( ns + "Execution", new XAttribute( "id", "E3" ) ),
        new XAttribute( "name", "Test3" ),
        new XAttribute( "id", "T3" ) );
    doc.Root.Element( ns + "TestDefinitions" ).Add( unitTest );
    
    doc.Save( "tests.xml" );
    

    To add the element you must create the XElement object and pass to its constructor the name of the new element and all its stuff like child elements, attributes (through a comma). Then you must specify where you want to add new element: by going from the Root element through the XML three (like in this example) or by query.

    You can find needed element by LINQ query. Next example shows how to get all TestEntries from the TestList with id L1:

    var query = from e in doc.Root.Elements( ns + "TestEntries" ).Elements()
                where e.Attribute( "testListId" ).Value == "L1"
                select new
                {
                    TestId = e.Attribute( "testId" ).Value,
                    ExecutionId = e.Attribute( "executionId" ).Value
                };
    
    foreach ( var testEntry in query )
    {
        Console.WriteLine( testEntry.TestId + " " + testEntry.ExecutionId );
    }
    

    Result objects from this query have anonymous type with useful properties. If you want work with XElement objects, simply change select new ... to select e.

    If you want update the value of the element, find it (look above) and call the SetValue() method.

    If you working with namespaces (like you file) you must create XNamespace object with needed value and concatenate it with all elements’ names that you need.

    To save your changes to the xml file on dist, call Save() method.

    LINQ to XML Overview in MSDN

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following xml structure and want to add it to a treeview.
I have the following XML structure: <Main> <Node1>Definite</Node1> <Node2>Definite</Node2> <Node3>Definite</Node3> <Node4>Definite</Node4> <Node5>Definite</Node5> <Node6>Definite</Node6> <A>Possible</A>
I have the following sample XML structure: <SavingAccounts> <SavingAccount> <ServiceOnline>yes</ServiceOnline> <ServiceViaPhone>no</ServiceViaPhone> </SavingAccount> <SavingAccount> <ServiceOnline>no</ServiceOnline>
I have the following XML structure so that I can add my configuration details
I have following XML generated by serializing a boost::posix_time::ptime structure. I want to create
I want have the following output: <?xml version=1.0 encoding=UTF-8?> <structure:structuralDataRoot xmlns:register=http://www.test.ch/register/1 xmlns:structure=http://test.ch/structure/1 > <structure:tester>ZH</structure:tester>
I have the following XML source structure: <turnovers> <turnover repid=1 amount=500 rate=0.1/> <turnover repid=5
I have the following simplified XML structure: <?xml version="1.0" encoding="UTF-8" ?> <INVOIC02> <IDOC BEGIN="1">
I have following object structure, deseralized from XML (WS): <ns2:Category> <ns2:CategoryId>800003</ns2:CategoryId> <ns2:CategoryName>Name1</ns2:CategoryName> <ns2:Categories> <ns2:Category>
I have a large xml file (approx. 10 MB) in following simple structure: <Errors>

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.