I'm using C# (WPF).
I have the following XML file:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationToRun IsFromXML="true">
<Apps>
<FulllPath>c:\file1.txt</FullPath>
<FulllPath>c:\file2.txt</FullPath>
<FulllPath>c:\file3.txt</FullPath>
</Apps>
</ApplicationToRun>
And i try to deseriazlier the xml file.My Code:
mlSerializer xs = new XmlSerializer(typfof(MyXMLClass));
StringReader st = new StringReader(xmlPath);
MyXMLClass apps = (MyXMLClass)xs.Deserialize(st); //Exception - System.Windows.Markup.XamlParseException
Inside the inner exception: System.InvalidOperationException :There is an error in XML document (1.1) ...
My Classes:
[XmlRootAttriute("ApplicationToRun:)
public class MyXMLClass
{
[XmlAttribute]
public bool IsFromXML {get;set;}
[XmlElement("Apps")]
public FullPath [] fullPath {get;set;}
}
public class FullPath
{
public stirng fullPathApp {set;get;}
}
Where is my mistake?
Thanks!
The XML is badly formed, but all it takes is a bit of cleaning up:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationToRun IsFromXML="true">
<Apps>
<FullPath>foo</FullPath>
<FullPath>bar</FullPath>
</Apps>
</ApplicationToRun>
Then assign the correct XML attributes to your class:
[XmlRootAttribute("ApplicationToRun")]
public class MyXMLClass
{
[XmlAttribute]
public bool IsFromXML { get; set; }
[XmlArray("Apps")]
[XmlArrayItem("FullPath")]
public string[] fullPath { get; set; }
}
There's a typo in the elements. The opening elements have 3 Ls (FulllPath) but the closing ones have 2 Ls (as they should) that's causing the XML error
Related
I am trying to deserialize the XML contents of the CCTray.xml from my build server with c#.
Tje XML content has a parent 'Projects' element with an array of 'Project' elements below. Each project element has multiple attributes.
I created a Projects model:
[XmlRoot("Projects")]
public class ProjectCollection
{
public ProjectCollection() { Projects = new List<Project>(); }
[XmlArray("Project")]
public List<Project> Projects { get; set; }
}
And a model for each project:
public class Project
{
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("Activity")]
public string Activity { get; set; }
[XmlElement("lastBuildStatus")]
public string LastBuildStatus { get; set; }
[XmlElement("lastBuildLabel")]
public string LastBuildLabel { get; set; }
[XmlElement("WebUrl")]
public string WebUrl { get; set; }
[XmlElement("LastBuildTime")]
public DateTimeOffset LastBuildTime { get; set; }
}
I try and deserialize the response from HttpClient like:
var xmlStream = await response.Content.ReadAsStreamAsync();
var serializer = new XmlSerializer(typeof(ProjectCollection));
var projects = (ProjectCollection)serializer.Deserialize(xmlStream);
The result of which is an empty ProjectCollection object.
I'm know I must be mapping the XML to my object incorrectly, but I can't see where its wrong.
A sample of the XML being returned is:
<?xml version="1.0" encoding="utf-8"?>
<Projects>
<Project name="Company.Project1.Api :: Build" activity="Sleeping" lastBuildStatus="Success" lastBuildLabel="1.0.0.203" lastBuildTime="2017-03-21T10:56:53" webUrl="http://build-company.com:8153/go/pipelines/Company.Project1.Api/203/Build/1" />
<Project name="Company.Project2.Api :: Build :: Build" activity="Sleeping" lastBuildStatus="Success" lastBuildLabel="1.0.0.203" lastBuildTime="2017-03-21T10:56:53" webUrl="http://build-company.com:8153/go/tab/build/detail/Company.Project2.Api/203/Build/1/Build" />
</Projects>
What do I need to do deserialize this XML to my object correctly?
Thanks
Your class structure does not fit the data you get. With your class structure the XML struture should look different. The root needs an other element called Project. This element contains your items.
<?xml version="1.0" encoding="utf-8"?>
<Projects>
<Project>
<Project name="Company.Project1.Api :: Build" activity="Sleeping" lastBuildStatus="Success" lastBuildLabel="1.0.0.203" lastBuildTime="2017-03-21T10:56:53" webUrl="http://build-company.com:8153/go/pipelines/Company.Project1.Api/203/Build/1" />
<Project name="Company.Project2.Api :: Build :: Build" activity="Sleeping" lastBuildStatus="Success" lastBuildLabel="1.0.0.203" lastBuildTime="2017-03-21T10:56:53" webUrl="http://build-company.com:8153/go/tab/build/detail/Company.Project2.Api/203/Build/1/Build" />
</Project>
</Projects>
To get the data, based on your XML structure, correctly you have to use a simple root like
[XmlRoot("Projects")]
public class ProjectCollection : List<Project>
{
}
Also you need to update all properties of your Project. The XML Project does not have any elements, only XML attributes are used to store data. This means you need to use XmlAttribute instead of XmlElement. Also note that the names are case sensitive, which is also not correct in your code.
[XmlAttribute("name")]
public string Name { get; set; }
//...
Trying to deserialize a XML log file. And cannot seem to get anything but Error in XML Document(0,0). I am guessing it has something to do with my class but I cannot seem to find a solution. I cannot change the XML formatting as this is a log file coming from a server (just simplified)
XML
<?xml version="1.0" encoding="utf-8"?>
<POSLog>
<Transaction>
<RetailStoreID>1</RetailStoreID>
<SequenceNumber>2</SequenceNumber>
</Transaction>
<Transaction>
<RetailStoreID>1</RetailStoreID>
<SequenceNumber>3</SequenceNumber>
</Transaction>
</POSLog>
Class
[Serializable()]
public class Transaction
{
[XmlElement("RetailStoreID")]
public string RetailStoreID { get; set; }
[XmlElement("SequenceNumber")]
public string SequenceNumber { get; set; }
}
[Serializable()]
[XmlRoot("POSLog")]
public class POSLog
{
[XmlArray("POSLog")]
[XmlArrayItem("Transaction", typeof(Transaction))]
public Transaction[] Transaction { get; set; }
}
Deserialize code
POSLog poslog = new POSLog();
string path = "POSLog.xml";
XmlSerializer serializer = new XmlSerializer(typeof(POSLog));
StreamReader reader = new StreamReader(path);
poslog = (POSLog)serializer.Deserialize(reader);
Found fix by switching from StreamReader to FileStream along with changes to the class. Changing encoding didn't seem to help when using the StreamReader.
I cannot reproduce the problem you are seeing. However, there is an issue with the POSLog class -- it needs to be defined as follows:
[Serializable()]
[XmlRoot("POSLog")]
public class POSLog
{
[XmlElement("Transaction")]
public Transaction[] Transaction { get; set; }
}
Your XML has a root element <POSLog> containing a repeating sequence of <Transaction> elements. [XmlElement("Transaction")] maps the array to just such a one-level repeating sequence.
Example fiddle.
Changing the Class to the follow above answer
[Serializable()]
[XmlRoot("POSLog")]
public class POSLog
{
[XmlElement("Transaction")]
public Transaction[] Transaction { get; set; }
}
Along with Changing the following StreamReader lines
StreamReader reader = new StreamReader(path);
poslog = (POSLog)serializer.Deserialize(reader);
To this:
FileStream fs = new FileStream(path, FileMode.Open);
poslog = (POSLog)serializer.Deserialize(fs);
Fixed the issue I was having with the root element and I was able to deserialize the XML. Thanks to dbc for help in getting me started solving a solution for the first time in a forum!
I am new to unit testing and I am wondering what would be the best practices for unit testing xml deserialisation.
Consider the following xml:
<people>
<person id="1">
<name>Joe</name>
<age>28</age>
</person>
<person id="2">
<name>Jack</name>
<age>38</age>
</person>
</people>
And the following model class for the people:
[XmlRoot(ElementName ="people")]
public class People
{
public People() { PeopleList = new List<Person>(); }
[XmlElement("person")]
public List<Person> PeopleList { get; set; }
}
public class Person
{
[XmlAttribute("id")]
public int id { get; set; }
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("age")]
public int Age { get; set; }
}
I deserialize the xml using:
public List<Person> GetListOfPeople()
{
People plist = new People();
string content;
using (StreamReader sr = new StreamReader(manager.Open("People.xml")))
{
var serializer = new XmlSerializer(typeof(People));
plist = (People)serializer.Deserialize(sr);
}
return plist.PeopleList;
}
What would be the best methods to unit test the GetListOfPeople method above?
If you can change your method to take an xml file as an input parameter, you can have a sample xml file created and added to your test project. Since you know the values of your xml file, you can start comparing the values directly.
Considering you'll use the sample file you provided in your test, you can verify like this:
var persons = x.GetListOfPeople("sample.xml");
Assert.AreEqual("Joe", persons[0].Name);
Assert.AreEqual(38, persons[1].Age);
If the xml file is coming to your code from some source and you think it couldn't be following your xml schema all the time, then probably you can create some sample xml files again which violate your schema and prepare tests to call your method which should throw some exception if schema is not correct.
Hope this helps.
I was using this Xml Deserialization with complex elements in c# as a reference. I've noticed there are a lot of threads about Deserializing xml, but they don't mention when a tag has multiple values within it.
I am trying to deserialize my xml for lvl3 into an array of objects.
I'm getting a "there is an error in the xml document (1, 2)" error.
I have an xml string that I'm retrieving via an HTTP GET request that is formatted like this:
<xml ...>
<lvl1 id="xxx" name="yyy">
<lvl2 id="aaa" name="bbb">
<lvl3 id="mmm" name="nnn" val1="ppp" val2="qqq">
<lvl4a x="000" y="000" z="000" />
<lvl4b a="000" b="000" c="000" />
<lvl4c l="000" w="000" h="000" />
...
</lvl3>
</lvl2>
</lvl1>
</xml>
I have the following code that keeps throwing an exception:
"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll"
The exception is thrown by this line:
temp = (Test)new XmlSerializer(typeof(Test)).Deserialize(rdr);
But I'm not sure how to go about debugging it to find the error. Here is the complete code:
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlstring);
XmlNodeList list = xmldoc.GetElementsByTagName("lvl2");
for (int i = 0; i < list.Count; i++)
{
Test temp = new Test();
using (XmlReader rdr = XmlReader.Create(new StringReader(list[i].InnerXml)))
{
temp = (Test)new XmlSerializer(typeof(Test)).Deserialize(rdr); // exception thrown here
}
Console.WriteLine(temp.id);
Console.WriteLine(temp.overall.x);
}
[XmlRoot("lvl3")]
public class Test{
[XmlAttribute("id")]
public string id { get; set; }
[XmlAttribute("name")]
public string name { get; set; }
[XmlElement(ElementName = "lvl4a")]
public Overall overall { get;set; }
}
public class Overall
{
[XmlAttribute("x")]
public string x { get; set; }
[XmlAttribute("y")]
public string y { get;set; }
[XmlAttribute("z")]
public string z { get;set; }
}
Either fix your Test class to include public properties for val1 and val2 attributes or remove them from the xml. The schema of xml should match the class structure.
I have XML file like this:
<?xml version="1.0"?>
<catalog>
<book id="1" date="2012-02-01">
<title>XML Developer's Guide</title>
<price>44.95</price>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="2" date="2013-10-16">
<author>Mark Colsberg</author>
<title>Dolor sit amet</title>
<price>5.95</price>
<description>Lorem ipsum</description>
</book>
</catalog>
How to quick convert it to C# classes to use access data by LINQ?
Do I have to write the class manually for any XML file case?
What about JSON format?
Is the XSD the only solution?
You have two possibilities.
Method 1. XSD tool
Suppose that you have your XML file in this location C:\path\to\xml\file.xml
Open Developer Command Prompt
You can find it in Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools
Or if you have Windows 8 can just start typing Developer Command Prompt in Start screen
Change location to your XML file directory by typing cd /D "C:\path\to\xml"
Create XSD file from your xml file by typing xsd file.xml
Create C# classes by typing xsd /c file.xsd
And that's it! You have generated C# classes from xml file in C:\path\to\xml\file.cs
Method 2 - Paste special
Required Visual Studio 2012+
Copy content of your XML file to clipboard
Add to your solution new, empty class file (Shift+Alt+C)
Open that file and in menu click Edit > Paste special > Paste XML As Classes
And that's it!
Usage
Usage is very simple with this helper class:
using System;
using System.IO;
using System.Web.Script.Serialization; // Add reference: System.Web.Extensions
using System.Xml;
using System.Xml.Serialization;
namespace Helpers
{
internal static class ParseHelpers
{
private static JavaScriptSerializer json;
private static JavaScriptSerializer JSON { get { return json ?? (json = new JavaScriptSerializer()); } }
public static Stream ToStream(this string #this)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(#this);
writer.Flush();
stream.Position = 0;
return stream;
}
public static T ParseXML<T>(this string #this) where T : class
{
var reader = XmlReader.Create(#this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
return new XmlSerializer(typeof(T)).Deserialize(reader) as T;
}
public static T ParseJSON<T>(this string #this) where T : class
{
return JSON.Deserialize<T>(#this.Trim());
}
}
}
All you have to do now, is:
public class JSONRoot
{
public catalog catalog { get; set; }
}
// ...
string xml = File.ReadAllText(#"C:\path\to\xml\file.xml");
var catalog1 = xml.ParseXML<catalog>();
string json = File.ReadAllText(#"C:\path\to\json\file.json");
var catalog2 = json.ParseJSON<JSONRoot>();
Here you have some Online XML <--> JSON Converters: Click
You can follow this simple step
1.Please Add using System.Xml as a reference;
2.Make a class named book in this way
public class book
{
public Nullable<System.DateTime> date{ get; set; }
public decimal price { get; set; }
public string title { get; set; }
public string description { get; set; }
}
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Write down full path");
XmlNodeList dataNodes = xmlDoc.SelectNodes("/catalog");
foreach (XmlNode node in dataNodes)
{
book objbook = new book();
objbook.date=Convert.ToDateTime(node.Attributes["date"].Value);
objbook.title=node.SelectSingleNode("title").InnerText;
objbook.description=node.SelectSingleNode("description").InnerText;
objbook.price=Convert.ToDecimal(node.SelectSingleNode("price").InnerText);
}
}
catch(Exception ex)
{
throw ex;
}
Use the XML Schema Definition Tool xsd.exe found in your framework tools to convert your schema into a serializable class or dataset.
xsd file.xsd {/classes | /dataset} [/element:element]
[/language:language] [/namespace:namespace]
[/outputdir:directory] [URI:uri]
And in example, whereas the C# class will be generated in the same directory as the xsd tool:
xsd /c YourFile.xsd
Use the super simple way using 'Paste XML As Classes' functionality in Visual studio menu.
1.copy the xml source in the clipboard, something like CTRL+A and CTRL+C
2.Go to 'Edit' Menu -> Paste Special -> Paste XML As Classes, to paste the generated classes based on the source xml"
Ref: More steps in detail at this link
Here is one another easy way to parse xml file using Cinchoo ETL, an open source library
Define POCO class as below
public class Book
{
[ChoXPath("#id")]
public int Id { get; set; }
[ChoXPath("#date")]
public DateTime Date { get; set; }
[ChoXPath("author")]
public string Author { get; set; }
[ChoXPath("title")]
public string Title { get; set; }
[ChoXPath("price")]
public double Price { get; set; }
[ChoXPath("description")]
public string Description { get; set; }
}
Use it to the parser as below
using (var r = new ChoXmlReader<Book>("*** Xml file path ***")
.WithXPath("//catalog/book", true)
)
{
foreach (var rec in r)
rec.Print();
}
Sample fiddle: https://dotnetfiddle.net/3UI82F