Monday, December 16, 2013

Want to use DataSet and Linq to read XML?

Here is a small example on how to use DataSet and Linq to read XML. It may be helpful when dealing with small and large schema XML.

Example:
1.        
XML schema:
<Main>
  <Node1 Name="Test1" Attrib1="Attrib1Value" Attrib2="Attrib2Value"></Node1>
  <Node1 Name="Test2" Attrib1="Attrib1Value" Attrib2="Attrib2Value"></Node1>
  <Node1 Name="Test3" Attrib1="Attrib1Value" Attrib2="Attrib2Value"></Node1>
</Main>

Virtual Data Table representation (Each unique node represented as a table and same kind of nodes as its rows with columns representing attributes):
Table Name: Node1
Node1_ID
Name
Attrib1
Attrib1
0
Test1
Attrib1Value
Attrib2Value
1
Test2
Attrib1Value
Attrib2Value
2
Test3
Attrib1Value
Attrib2Value

C# code to access and query data from above XML:
            string xmlfile = @"Example_1.xml";
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(xmlfile);

            DataTable node1Table = dataSet.Tables["Node1"];//Read XML node named = Node1
            string testName = "Test1";
            var query = from table in node1Table.AsEnumerable()
                        where table.Field<string>("Name").Equals(testName) //Selection criteria
                        select new
                        {
                                    //Return values
                                name = table.Field<string>("Name"),
                                attribVal1 = table.Field<string>("Attrib1"),
                                attribVal2 = table.Field<string>("Attrib2")
                        };

            //Print output
            foreach (var result in query)
            {
                Console.WriteLine("Name = " + result.name);
                Console.WriteLine("Attrib1 = " + result.attribVal1);
                Console.WriteLine("Attrib2 = " + result.attribVal2);
            }

Output:
Name = Test1
Attrib1 = Attrib1Value
Attrib2 = Attrib2Value
----------------------------


2.       Example:
XML:
<ApicOnlyGamesInfo>
  <TestInfo Test="Battlefield 3 - DirectX 11- Lighting Effect (Automation)" GameName="BattleField3" DxVersion="DX11" email="vgoel@nvidia.com">
    <NvCplSettingInfo Name="" Value=""/>
    <ApicTraceInfo GoldenImagePath="ApicOnlyGames\BF3\2560x1600\Lighting_Comrades" ApicPlayeName="d3d11_replayer.exe" PixellDiff="1000" Fuzz="5">
      <XP TraceSourcePath="ApicOnlyGames\APIC\BF3\COMRADES\Lighting\2560x1600" />
      <WIN7 TraceSourcePath="ApicOnlyGames\APIC\BF3\COMRADES\Lighting\2560x1600" />
      <WIN8 TraceSourcePath="ApicOnlyGames\APIC\BF3\COMRADES\Lighting\2560x1600" />
      <WINBLUE TraceSourcePath="ApicOnlyGames\APIC\BF3\COMRADES\Lighting\2560x1600" />
    </ApicTraceInfo>
    <ApicTraceInfo GoldenImagePath="ApicOnlyGames\BF3\2560x1600\Lighting_Operation_GUILLOTINE" ApicPlayeName="d3d11_replayer.exe" PixellDiff="1000" Fuzz="5">
      <XP TraceSourcePath="ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Lighting\2560x1600" />
      <WIN7 TraceSourcePath="ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Lighting\2560x1600" />
      <WIN8 TraceSourcePath="ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Lighting\2560x1600" />
      <WINBLUE TraceSourcePath="ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Lighting\2560x1600" />
    </ApicTraceInfo>
    <ApicTraceInfo GoldenImagePath="ApicOnlyGames\BF3\2560x1600\Lighting_SEMPER_FIDELIS" ApicPlayeName="d3d11_replayer.exe" PixellDiff="1000" Fuzz="5">
      <XP TraceSourcePath="ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Lighting\2560x1600" />
      <WIN7 TraceSourcePath="ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Lighting\2560x1600" />
      <WIN8 TraceSourcePath="ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Lighting\2560x1600" />
      <WINBLUE TraceSourcePath="ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Lighting\2560x1600" />
    </ApicTraceInfo>
  </TestInfo>
 
  <TestInfo Test="Battlefield 3 - DirectX 11- Shadows (Automation)" GameName="BattleField3" DxVersion="DX11" email="vgoel@nvidia.com">
    <NvCplSettingInfo Name="" Value=""/>   
    <ApicTraceInfo GoldenImagePath="ApicOnlyGames\BF3\2560x1600\Shadows_Operation_GUILLOTINE" ApicPlayeName="d3d11_replayer.exe" PixellDiff="1000" Fuzz="5">
      <XP TraceSourcePath="ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Shadows\2560x1600" />
      <WIN7 TraceSourcePath="ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Shadows\2560x1600" />
      <WIN8 TraceSourcePath="ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Shadows\2560x1600" />
      <WINBLUE TraceSourcePath="ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Shadows\2560x1600" />
    </ApicTraceInfo>
    <ApicTraceInfo GoldenImagePath="ApicOnlyGames\BF3\2560x1600\Shadows_SEMPER_FIDELIS" ApicPlayeName="d3d11_replayer.exe" PixellDiff="1000" Fuzz="5">
      <XP TraceSourcePath="ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Shadows\2560x1600" />
      <WIN7 TraceSourcePath="ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Shadows\2560x1600" />
      <WIN8 TraceSourcePath="ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Shadows\2560x1600" />
      <WINBLUE TraceSourcePath="ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Shadows\2560x1600" />
    </ApicTraceInfo>
  </TestInfo>
</ApicOnlyGamesInfo>

Virtual Data Table Representation (Each unique node represented as a table and same kind of nodes as its rows with columns representing attributes):
     
      Table: TestInfo
TestInfo_ID
Test
GameName
DxVersion
email
0
Battlefield 3 - DirectX 11- Lighting Effect (Automation)
BattleField3
DX11
1
Battlefield 3 - DirectX 11- Shadows (Automation)
BattleField3
DX11

      Table: ApicTraceInfo
TestInfo_ID
ApicTraceInfo_ID
GoldenImagePath
ApicPlayeName
PixellDiff
Fuzz
0
0
ApicOnlyGames\BF3\2560x1600\Lighting_Comrades
d3d11_replayer.exe
1000
5
0
1
ApicOnlyGames\BF3\2560x1600\Lighting_Operation_GUILLOTINE
d3d11_replayer.exe
1000
5
0
2
ApicOnlyGames\BF3\2560x1600\Lighting_SEMPER_FIDELIS
d3d11_replayer.exe
1000
5
1
3
ApicOnlyGames\BF3\2560x1600\Shadows_Operation_GUILLOTINE
d3d11_replayer.exe
1000
5
1
4
ApicOnlyGames\BF3\2560x1600\Shadows_SEMPER_FIDELIS
d3d11_replayer.exe
1000
5

      Table: XP
ApicTraceInfo_ID
XP_ID
TraceSourcePath
0
0
ApicOnlyGames\APIC\BF3\COMRADES\Lighting\2560x1600
1
1
ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Lighting\2560x1600
2
2
ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Lighting\2560x1600
3
3
ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Shadows\2560x1600
4
4
ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Shadows\2560x1600

Similarly there will be tables for WIN7/WIN8 and WINBLUE as well.

C# code to access and query from above xml (Get TraceSourcePath from each ApicTraceInfo for matching TestInfo->Test):
string xmlFile = @"C:\Perforce\sw\qa\AtpTestcases\GamesAutomation\ApicOnlyGames\ApicOnlyGamesInputData.xml";
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(xmlFile);

            DataTable testInfo = dataSet.Tables["TestInfo"];
            DataTable apicTraceInfo = dataSet.Tables["ApicTraceInfo"];
            DataTable os = dataSet.Tables[Detection.GetOS().Replace("32", "").Replace("64", "")];

            string testName = "Battlefield 3 - DirectX 11- Shadows (Automation)";
            var query = from test in testInfo.AsEnumerable()
                        join traceInfo in apicTraceInfo.AsEnumerable()
                        on test.Field<int>("TestInfo_ID") equals
                        traceInfo.Field<int>("TestInfo_ID")
                        join osNode in os.AsEnumerable()
                        on traceInfo.Field<int>("ApicTraceInfo_ID") equals
                        osNode.Field<int>("ApicTraceInfo_ID")
                        where test.Field<string>("Test").Equals(testName)//Selection criteria
                        select new
                        {
                              //Return values
                            Test = test.Field<string>("Test"),
                            GoldenPath = traceInfo.Field<string>("GoldenImagePath"),
                            tracePath = osNode.Field<string>("TraceSourcePath")
                        };

            //Print output
            foreach (var res in query)
            {
                Console.WriteLine("Test Name: " + res.Test);
                Console.WriteLine("GoldenPath: " + res.GoldenPath);
                Console.WriteLine("Trace Path : " + res.tracePath);
      }

Output:
Test Name: Battlefield 3 - DirectX 11- Shadows (Automation)
GoldenPath: ApicOnlyGames\BF3\2560x1600\Shadows_Operation_GUILLOTINE
Trace Path : ApicOnlyGames\APIC\BF3\Operation_GUILLOTINE\Shadows\2560x1600
Test Name: Battlefield 3 - DirectX 11- Shadows (Automation)
GoldenPath: ApicOnlyGames\BF3\2560x1600\Shadows_SEMPER_FIDELIS
Trace Path : ApicOnlyGames\APIC\BF3\SEMPER_FIDELIS\Shadows\2560x1600

--------------------------------------------------------------------------------------------------------

No comments:

Post a Comment