Parse xml to object - c#

I'm trying to convert my xml file to a list of objects.
private void ReadChangelog()
{
XmlSerializer serializer = new XmlSerializer(typeof(Changelog));
Changelog changelog = (Changelog)serializer.Deserialize(new StringReader("changelog.xml"));
foreach (Release release in changelog.Releases)
{
string version = release.Version;
string date = release.Date;
string changes = release.Changes;
}
}
This is my changelog class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
[XmlRoot()]
public class Changelog
{
private Release[] releases;
public Release[] Releases
{
get { return releases; }
set { releases = value; }
}
}
public class Release
{
private string version;
private string date;
private string changes;
[XmlAttribute]
public string Version
{
get { return version; }
set { version = value; }
}
[XmlAttribute]
public string Date
{
get { return date; }
set { date = value; }
}
[XmlAttribute]
public string Changes
{
get { return changes; }
set { changes = value; }
}
}
And this is my xml
<?xml version="1.0" encoding="utf-8" ?>
<Changelog>
<Releases>
<Release>
<Version>1511</Version>
<Date>25-11-2013</Date>
<Changes>
qzdqzdqzdqzdzdzzqefrsrgsrg
</Changes>
</Release>
<Release>
<Version>1510</Version>
<Date>25-11-2013</Date>
<Changes>
Blabqzdzqdqzdqzd
</Changes>
</Release>
<Release>
</Releases>
</Changelog>
But when I try to run this I get an error
InvalidOperationException, The xml document (1,1) contains an error.
What am I doing wrong?

new StringReader("changelog.xml") doesn't read in the contents of changelog.xml. Instead, this code leaves you trying to deserialize the literal 'changelog.xml' string. I think you want something like new StreamReader("changelog.xml").

StringReader creates stream from string content, not from the file with given path. User StreamReader instead.
Changelog changelog = (Changelog)serializer.Deserialize(new StreamReader("changelog.xml"));
Despite of that, there are couple more errors in your code:
Your document uses elements, and your class declaration uses XmlAttributeAttribute. It won't work together.
Your document is not a correct XML document. It has some elements that are not closed.

Related

Tricky XML Manipulation: Create an element out of its own and other sibling's data

I have this replicate scenario my XML document below:
<?xml version="1.0" encoding="utf-8"?>
<Home>
<Kitchen>
<Pantry>
<Ingredients>
<Name>Tomato</Name>
<ID>1</Price_ID>
<Name>Tomato</Name>
<Income>Sales</Income> // replace the <Income> element with its value <Sales>
<Cost>Materials</Cost>
<Price>100</Price> // the value for new <Sales> element shall be this <Price> value
</Ingredients>
//.. and thousands more sets of ingredients
</Pantry>
</Kitchen>
</Home>
//.. and thousands more sets of ingredients
And I want to restructure it in the following manner:
<?xml version="1.0" encoding="utf-8"?>
<Home>
<Kitchen>
<Pantry>
<Ingredients>
<Name>Tomato</Name>
<ID>1</ID>
<Name>Tomato</Name>
<Sales>100</Sales> // the <Income> was replaced by its value and the value was taken from the <Price> element that was deleted also
<Cost>Materials</Cost>
</Ingredients>
//.. and thousands more sets of ingredients
</Pantry>
</Kitchen>
</Home>
I'm still trying to figure out how I'm going to do this. I will appreciate any help here.
Using Xml Ling :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication37
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<XElement> ingredients = doc.Descendants("Ingredients").ToList();
foreach (XElement ingredient in ingredients)
{
XElement xIncome = ingredient.Element("Income");
XElement xPrice = ingredient.Element("Price");
xIncome.ReplaceWith(new XElement("Sales", (string)xPrice));
xPrice.Remove();
}
}
}
}
Firstly create a Class for the new Model
public class NewIngredients
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Sales { get; set; }
public string Cost{ get; set; }
}
Presuming the Xml Document is in a file called Kitchen.xml
XElement Kitchen = XElement.Load(#"Kitchen.xml");
then use Linq to Xml to create your new model from the old something like this (Note probably need to check for nulls etc)
var newIngredients = Kitchen.Descendants("Ingredients").Select(x => new NewIngredients
{
Id = int.Parse(x.Element("ID").Value),
Name = x.Element("Name").Value,
Sales = decimal.Parse(x.Element("Price").Value)
Cost = x.Element("Cost").Value
});
Convert back to xml if needed
var serializer = new XmlSerializer(newIngredients.First().GetType());
serializer.Serialize(Console.Out, newIngredients.First()); //Printed to console but could move to file if needed

C# XML parsing thru EPPlus

I am parsing XML, I have generated Class from XSD file, I am able to access simple values from XML, but I don't know how to get value from this. I need it as String.
public partial class AccountIdentification4Choice {
private object itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("IBAN", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("Othr", typeof(GenericAccountIdentification1))]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
In XML value can be like this:
<CdtrAcct><Id><Othr><Id>BBAN</Id></Othr></Id></CdtrAcct>
or
<CdtrAcct><Id><IBAN>BBAN</IBAN></Id></CdtrAcct>
Update - all resource files:
XML: Priklad_multi.XML
XSD: camt.053.001.02.xsd
Then I parse XML with EPPlus package:
XmlSerializer ser = new XmlSerializer(typeof(Document));
Document resp = ser.Deserialize(new FileStream(file, FileMode.Open)) as Document;
And then I can access XML values like this (for example Stmt-Acct)
resp.BkToCstmrStmt.Stmt[ind].Acct.Id.Item
But I don't know how to access this CdtrAcct section in same way.
Your question is not clear, but as I understand you want to get IBAN tag, right? If so, you could do that easily with Linq To XML. ie:
void Main()
{
string xml1 = "<CdtrAcct><Id><Othr><Id>BBAN</Id></Othr></Id></CdtrAcct>";
string xml2 = "<CdtrAcct><Id><IBAN>BBAN</IBAN></Id></CdtrAcct>";
Console.WriteLine(CheckIban(xml1));
Console.WriteLine(CheckIban(xml2));
}
private string CheckIban(string xml)
{
var iban = XElement.Parse(xml).DescendantsAndSelf("IBAN").FirstOrDefault();
if (iban != null)
{
return (string)iban;
}
iban = XElement.Parse(xml).DescendantsAndSelf("Othr")
.FirstOrDefault();
if (iban != null)
{
return iban.Element("Id").Value;
}
return null;
}

Deserialize dependent on field value

I need to deserialize XML that uses a field "type" to indicate what content to expect.
Type 0 says that I can expect simple text whilst type 1 indicates that the content is of a more complex structure.
I know that I could write some custom deserialization mechanism but would like to know whether there was any builtin way to solve this.
Since the XMLSerializer expects a string it simply throws away the content in case it is XML. This stops me from running the content deserialization as a second step.
<Msg>
<MsgType>0</MsgType>
<Data>Some text</Data>
</Msg>
<Msg>
<MsgType>1</MsgType>
<Data>
<Document>
<Type>PDF</Type>
.....
</Document>
</Data>
</Msg>
That isn't supported out of the box; however, you could perhaps use:
public XmlNode Data {get;set;}
and run the "what to do with Data?" as a second step, once you can look at MsgType.
Complete example:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
static class P
{
static void Main()
{
const string xml = #"<Foo>
<Msg>
<MsgType>0</MsgType>
<Data>Some text</Data>
</Msg>
<Msg>
<MsgType>1</MsgType>
<Data>
<Document>
<Type>PDF</Type>
.....
</Document>
</Data>
</Msg>
</Foo>";
var fooSerializer = new XmlSerializer(typeof(Foo));
var docSerializer = new XmlSerializer(typeof(Document));
var obj = (Foo)fooSerializer.Deserialize(new StringReader(xml));
foreach (var msg in obj.Messages)
{
switch (msg.MessageType)
{
case 0:
var text = msg.Data.InnerText;
Console.WriteLine($"text: {text}");
break;
case 1:
var doc = (Document)docSerializer.Deserialize(new XmlNodeReader(msg.Data));
Console.WriteLine($"document of type: {doc.Type}");
break;
}
Console.WriteLine();
}
}
}
public class Foo
{
[XmlElement("Msg")]
public List<Message> Messages { get; } = new List<Message>();
}
public class Message
{
[XmlElement("MsgType")]
public int MessageType { get; set; }
public XmlNode Data { get; set; }
}
public class Document
{
public string Type { get; set; }
}

XML to C# Class Question

Can someone please help me, I have this xml snippet
<?xml version="1.0" encoding="utf-8" ?>
<EmailConfiguration>
<DataBoxID>123</DataBoxID>
<DefaultSendToAddressCollection>
<EmailAddress>email#whereEver.com</EmailAddress>
</DefaultSendToAddressCollection>
</EmailConfiguration>
I want to create a corressponding c# class from this. Before you say - "Just use xsd.exe", the output from Xsd cannot be serialized and deserialized correct, because it generates the class using partial classes.
Please can you tell me how to create this class.... here is the approach I took, but it doesn't work.
public class EmailConfiguration
{
private string dataBoxID;
public string DataBoxID
{
get { return dataBoxID; }
set { dataBoxID = value; }
}
private DefaultSendToAddressCollectionClass defaultSendToAddressCollection;
public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection
{
get { return defaultSendToAddressCollection; }
set { defaultSendToAddressCollection = value; }
}
}
And here is the class declaration for the subclass
public class DefaultSendToAddressCollectionClass
{
private string[] emailAddress;
public string[] EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
}
Did you use VS2008's XSD?
Here's the output I got:
c:>xsd email.xml
Writing file 'c:\email.xsd'
c:>xsd email.xsd /c /edb
Writing file 'c:\email.cs'
Generates serializable output:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class EmailConfiguration : object, System.ComponentModel.INotifyPropertyChanged {
private string dataBoxIDField;
private EmailConfigurationDefaultSendToAddressCollection[] defaultSendToAddressCollectionField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string DataBoxID {
get {
return this.dataBoxIDField;
}
set {
this.dataBoxIDField = value;
this.RaisePropertyChanged("DataBoxID");
}
}
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(#"D:\file.xml");
var catalog1 = xml.ParseXML<catalog>();
string json = File.ReadAllText(#"D:\file.json");
var catalog2 = json.ParseJSON<JSONRoot>();
Here you have some Online XML <--> JSON Converters: Click
Bare minimum working... looks like you are only required to add one attribute.
public class EmailConfiguration
{
public string DataBoxID { get; set; }
public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection { get; set; }
}
public class DefaultSendToAddressCollectionClass
{
[XmlElement]
public string[] EmailAddress { get; set; }
}
Using .NET 3.5:
[XmlRoot]
public class EmailConfiguration
{
[XmlElement]
public string DataBoxID { get; set; }
[XmlElement]
public DefaultSendToAddressCollectionClass DefaultSendToAddressCollection { get; set; }
}
public class DefaultSendToAddressCollectionClass
{
[XmlElement]
public string[] EmailAddress { get; set; }
}
XSD.EXE is the tool that produces classes specifically for the purpose of XML Serialization. If it produces partial classes, that's because they work for XML Serialization. That's not what your problem is.
Try using XSD.EXE and serializing / deserializing. If you get an exception again, then please catch it and then post the results of ex.ToString().
This class will serialize the way you want. I changed your custom collection to a List and used the XmlArrayItem attribute to specify how each email address would be serialized. There are many such attributes to help you fine tune the serialization process.
[Serializable]
public class EmailConfiguration {
private string dataBoxID;
public string DataBoxID {
get { return dataBoxID; }
set { dataBoxID = value; }
}
private List<string> defaultSendToAddressCollection;
[XmlArrayItem("EmailAddress")]
public List<string> DefaultSendToAddressCollection {
get { return defaultSendToAddressCollection; }
set { defaultSendToAddressCollection = value; }
}
public EmailConfiguration() {
DefaultSendToAddressCollection = new List<string>();
}
}
XML serialization requires attributes. The way I've usually done it is to flag the class itself with [Serializable] and [XmlRoot], then mark up public properties with either [XmlElement], [XmlAttribute] or [NoSerialize].
What specific problem are you having?

.Net XmlSerializer: deserialize CDATA being inner text

I have a problem with CDATA deserialization using standard .Net XmlSerializer.
Update: I get XML from external system and I can't influence it's format so I can't make CData be enclosed in a separate Element of Attribute.
Serialization gives this:
<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><![CDATA[Hello, world!]]></MyClass>
Deserialization does not restore object to it's original state.
Here's class that is being serialized:
public class MyClass
{
string _data;
[XmlIgnore]
public string Data
{
get { return _data; }
set { _data = value; }
}
[XmlAnyElement]
public XmlCDataSection CData
{
get { return new XmlDataDocument().CreateCDataSection(Data); }
set { Data = value.Value; }
}
}
Here's the test which fails:
[Test]
public void CData_as_inner_text_test()
{
MyClass item = new MyClass();
item.Data = "Hello, world!";
XmlSerializer serializer = new XmlSerializer(item.GetType());
string serialized;
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, item);
serialized = sw.GetStringBuilder().ToString();
}
MyClass deserialized;
using (StringReader sr = new StringReader(serialized))
{
deserialized = (MyClass)serializer.Deserialize(sr);
}
Assert.AreEqual(item.Data, deserialized.Data); // For some reason, deserialized.Data == null
}
I found the same problem here but there's no answer:
XmlSerializer, XmlAnyElement and CDATA
The CData property ends up null, because the content of the CDATA section ends up in the Data property, where it is being ignored...
<MyClass><![CDATA[Hello, world!]]></MyClass>
is absolutely equivalent to:
<MyClass>Hello, world!</MyClass>
You shouldn't care whether the external app writes the content of MyClass as CData or not. Likewise, the external app shouldn't care how you write it out.
IOW, this should be all you need:
public class MyClass
{
string _data;
[XmlText]
public string Data
{
get { return _data; }
set { _data = value; }
}
}
First declare a property as XmlCDataSection
public XmlCDataSection ProjectXml { get; set; }
in this case projectXml is a string xml
ProjectXml = new XmlDocument().CreateCDataSection(projectXml);
when you serialize your message you will have your nice format (notice )
<?xml version="1.0" encoding="utf-16"?>
<MessageBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="Message_ProjectStatusChanged">
<ID>131</ID>
<HandlerName>Plugin</HandlerName>
<NumRetries>0</NumRetries>
<TriggerXml><![CDATA[<?xml version="1.0" encoding="utf-8"?><TmData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="9.0.0" Date="2012-01-31T15:46:02.6003105" Format="1" AppVersion="10.2.0" Culture="en-US" UserID="0" UserRole=""><PROJECT></PROJECT></TmData>]]></TriggerXml>
<MessageCreatedDate>2012-01-31T20:28:52.4843092Z</MessageCreatedDate>
<MessageStatus>0</MessageStatus>
<ProjectId>0</ProjectId>
<UserGUID>8CDF581E44F54E8BAD60A4FAA8418070</UserGUID>
<ProjectGUID>5E82456F42DC46DEBA07F114F647E969</ProjectGUID>
<PriorStatus>0</PriorStatus>
<NewStatus>3</NewStatus>
<ActionDate>0001-01-01T00:00:00</ActionDate>
</MessageBase>

Categories

Resources