meetjaydeep
meetjaydeep

Reputation: 1850

Manipulate xml through c#

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>

`

Upvotes: 1

Views: 894

Answers (1)

whyleee
whyleee

Reputation: 4049

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

Upvotes: 2

Related Questions