- First we need to convert the .xsd into C# classes with the Visual Studio program xsd.exe
- Open the 'Developer Command Prompt for VS2015'
- Execute the command:
- xsd.exe xsdfilename.xsd /outputdir:"{path}" /classes
- If you want to place the generated class in a namespace use /namespace:
- xsd.exe xsdfilename.xsd /classes /namespace:namespacename
- If you have multiple related .xsd-s (with identical children) then it's best to convert them all in one single xsd.exe call to prevent having a lot of duplicate declarations in Visual Studio which will have to be removed or commented out manually:
- xsd.exe xsdfile1.xsd xsdfile2.xsd xsdfile3.xsd /outputdir:"{path}" /classes
- Next is to import the class(es) into VS
- Then it's possible to 'deserialize' existing XML strings into the imported classes via the following code:
- XmlSerializer serializer = new XmlSerializer(typeof(XMLType));
- MemoryStream stream = new MemoryStream();
- StreamWriter writer = new StreamWriter(stream);
- writer.Write(XML_String);
- writer.Flush();
- stream.Position = 0;
- this.AX_Instance = (XMLType)serializer.Deserialize(stream);
- The next step is to serialize an XML object into a string via the following code:
- serializer.Serialize(stream, AX_Instance);
- byte[] newXmlArray = stream.ToArray();
- string newXml = System.Text.Encoding.Default.GetString(newXmlArray);
dinsdag 14 maart 2017
C# > Serializing and Deserializing XMLs
Testers generally need to generate lots of testdata. Often this testdata comes in the form of files. So one of the first areas where automation can really help out in the testing effort is in file generation. We're going to look into XML file reading and generation on the basis of the .xsd of the .xml.
vrijdag 3 maart 2017
C# > Why Properties Matter
interesting blog post
http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx
http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx
Abonneren op:
Reacties (Atom)