Search Unity

XML troubles.

Discussion in 'Scripting' started by KenClemson, Mar 14, 2017.

  1. KenClemson

    KenClemson

    Joined:
    Oct 6, 2016
    Posts:
    24
    Hi so I'm trying to output some of my data in XML, it's mostly outputting correctly but there's just one problem, the program requires me to have a strucure as following
    <?xml version="1.0" encoding="ISO-8859-7"?>
    <testcases>
    <testcase name="Kens Test Case">
    <summary>This is the data sent from the Gen_4 unit test</summary>
    <importance>4</importance>
    </steps>
    </testcase>
    </testcases>

    but when I run my script in unity I get

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfTestcase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <testcase name="test_1">
    <summary>testSummury</summary>
    <importance>4</importance>
    <steps />
    </testcase>
    </ArrayOfTestcase>

    Ok so the problem is that on the second line it should just say
    <testcases>
    But for some reason it keeps spitting out
    <ArrayOfTestcase>

    Here is my code, I hope someone can help me with this

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Xml.Serialization;
    4. using System.IO;
    5. using System.Collections.Generic;
    6.  
    7. public class UnitTestManager : MonoBehaviour
    8. {
    9.  
    10.     public class testcase
    11.     {
    12.         public testcase(string name, string summary, string importance, List<Step> steps)
    13.         {
    14.             this.name = name;
    15.             this.summary = summary;
    16.             this.importance = importance;
    17.             this.steps = steps;
    18.         }
    19.         public testcase() { }
    20.  
    21.         [XmlAttribute]
    22.         public string name;
    23.         public string summary;
    24.         public string importance;
    25.         public List<Step> steps;
    26.     }
    27.     public class Step
    28.     {
    29.         public Step()
    30.         {
    31.         }
    32.  
    33.         public Step(int step_number, string actions, string expectedResults)
    34.         {
    35.             this.step_number = step_number;
    36.             this.actions = actions;
    37.             this.expectedResults = expectedResults;
    38.         }
    39.  
    40.         public int step_number;
    41.         public string actions;
    42.         public string expectedResults;
    43.  
    44.     }
    45.     [XmlElement("testcases")]
    46.     private List<testcase> testcases;
    47.    
    48.  
    49.     void Serialize()
    50.     {
    51.         string screenshotFolder = "";
    52.         Config.Instance.GetValue(Config.SCREENSHOTFOLDER, ref screenshotFolder);
    53.  
    54.         XmlSerializer serial = new XmlSerializer(typeof(List<testcase>));
    55.  
    56.             TextWriter writer = new StreamWriter(screenshotFolder + "UnitTests.xml");
    57.             serial.Serialize(writer, testcases);      
    58.     }
    59.  
    60.     void Start()
    61.     {
    62.        
    63.         testcases = new List<testcase>();
    64.         testcases.Add(new testcase("test_1", "testSummury", "4", new List<Step>()));
    65.         Serialize();
    66.     }
    67. }