I'm using Visual Studio 2010(c#) and I am having some issues with my XML deserialization code. I can't get it to read my XML properly.
My XML reads as follows:
<?xml version="1.0" encoding="utf-8"?>
<command_strings version="1">
<commands>
<command cmd_id="1" state_id="1" label="On" cmd_type="fixed" cmd_string="1%0D" />
<command cmd_id="1" state_id="3" label="Off" cmd_type="fixed" cmd_string="0%0d" />
</commands>
</command_strings>
My code looks like:
[Serializable()]
public class Command
{
[System.Xml.Serialization.XmlAttribute("cmd_id")]
public int cmd_id { get; set; }
[System.Xml.Serialization.XmlAttribute("state_id")]
public int state_id { get; set; }
[System.Xml.Serialization.XmlAttribute("label")]
public string label { get; set; }
[System.Xml.Serialization.XmlAttribute("cmd_type")]
public string cmd_type { get; set; }
[System.Xml.Serialization.XmlAttribute("cmd_string")]
public string cmd_string { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("commands")]
public class CommandCollection
{
[XmlArray("commands")]
[XmlArrayItem("command", typeof(Command))]
public Command[] Command { get; set; }
}
public void XMLStrings(string myXML)
{
CommandCollection commandscollection = null;
XmlSerializer dserial = new XmlSerializer(typeof(CommandCollection));
StreamReader streamReader = new StreamReader(#"C:\123.xml");
commandscollection = (CommandCollection)dserial.Deserialize(streamReader);
streamReader.Close();
}
Any idea what I may be missing?
Any help will be greatly appreciated!
The CommandCollection class should be marked with the attribute [System.Xml.Serialization.XmlRoot("command_strings")].
You should also add a property for version and mark it with a XmlAttribute attribute.
Related
I started with the following code:
public class ChangeRestartProcessing
{
[XmlElement("ID")]
public long TransportId { get; set; }
[XmlElement("FI")]
public Information FinalInformation { get; set; }
}
... and while serialising, everything worked fine: FI was visible in the XML serialisation result.
Now, I've made following modification:
public class ChangeRestartProcessing
{
[XmlElement("ID")]
public long TransportId { get; set; }
[XmlElement("FI")]
public Information FinalInformation { get; set; }
[XmlElement("DI")]
public Information NormalInformation { get; set; }
}
The idea is to see the FI when that object exists, and to see DI when that object exists (it never happens that both are present).
However, now I don't see any FI or DI tag in my XML serialisation result.
Is that normal and what can I do in order to make the mentioned tags visible? Do I need to create a separate class for both cases or is there another approach?
Works fine here...
using System;
using System.Xml.Serialization;
var serializer = new XmlSerializer(typeof(ChangeRestartProcessing));
serializer.Serialize(Console.Out, // has ID+FI only
new ChangeRestartProcessing
{
FinalInformation = new() { Id = 42 },
});
Console.WriteLine();
serializer.Serialize(Console.Out, // has ID+DI only
new ChangeRestartProcessing
{
NormalInformation = new() { Id = 42 },
});
public class ChangeRestartProcessing
{
[XmlElement("ID")]
public long TransportId { get; set; }
[XmlElement("FI")]
public Information FinalInformation { get; set; }
[XmlElement("DI")]
public Information NormalInformation { get; set; }
}
public class Information
{
[XmlAttribute]
public int Id { get; set; }
}
output:
<?xml version="1.0" encoding="Codepage - 850"?>
<ChangeRestartProcessing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>0</ID>
<FI Id="42" />
</ChangeRestartProcessing>
<?xml version="1.0" encoding="Codepage - 850"?>
<ChangeRestartProcessing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>0</ID>
<DI Id="42" />
</ChangeRestartProcessing>
I am trying to take specific Nodes from an Xml and write it into a class. I have this.
public class TradeMark
{
[XmlElement]
public string MarkVerbalElementText { get; set; }
[XmlElement]
public int MarkCurrentStatusCode { get; set; }
[XmlElement]
public string ExpiryDate { get; set; } = "";
}
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(TradeMark));
using (TextReader reader = new StreamReader(pathToImportFile))
{
tradeMark = (TradeMark)serializer.Deserialize(reader);
}
}
In my Xml Data, there are more Node than just these 3. Now when i run the Code it says ...... was not expected. I guess bc. It tries to deserialize everything than only these 3 Infomartionen in Class TradeMark.
Can anyone help?
XML
<?xml version="1.0" encoding="UTF-8"?>
<Transaction xmlns="http://euipo.europa.eu/trademark/data" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://euipo.europa.eu/trademark/data http://euipo.europa.eu/schemas/trademark/EM-TM-TradeMark-V3-2.xsd">
<TransactionHeader>
<SenderDetails>
<RequestProducerDateTime>2018-08-18T15:33:35</RequestProducerDateTime>
</SenderDetails>
</TransactionHeader>
<TradeMarkTransactionBody>
<TransactionContentDetails>
<TransactionIdentifier>017690538</TransactionIdentifier>
<TransactionCode>EM-Trade Mark</TransactionCode>
<TransactionData>
<TradeMarkDetails>
<TradeMark operationCode="Insert">
<RegistrationOfficeCode>EM</RegistrationOfficeCode>
<ApplicationNumber>017690538</ApplicationNumber>
<ApplicationDate>2018-01-16</ApplicationDate>
<RegistrationDate>2018-06-14</RegistrationDate>
<ApplicationLanguageCode>en</ApplicationLanguageCode>
<SecondLanguageCode>es</SecondLanguageCode>
<ExpiryDate>2028-01-16</ExpiryDate>
<MarkCurrentStatusCode milestone="23" status="1">Registered</MarkCurrentStatusCode>
<MarkCurrentStatusDate>2018-06-15</MarkCurrentStatusDate>
<KindMark>Individual</KindMark>
<MarkFeature>Figurative</MarkFeature>
<TradeDistinctivenessIndicator>false</TradeDistinctivenessIndicator>
<WordMarkSpecification>
<MarkVerbalElementText>Tiens</MarkVerbalElementText>
</WordMarkSpecification>
Most likely this happens because your XML has a default namespace and Transaction is within this namespace.
You need to mark your class with XmlRootAttribute like so:
[XmlRootAttribute("TradeMark", Namespace="http://euipo.europa.eu/trademark/data",
IsNullable = false)]
public class TradeMark
XmlIgnore is what you're looking for.
MSDN Docs
See clarification in this answer, as the docs only state XmlIgnore will be ignored on serialize, but will also be ignored when deserializing.
From your example:
public class TradeMark
{
[XmlElement]
public string MarkVerbalElementText { get; set; }
[XmlElement]
public int MarkCurrentStatusCode { get; set; }
[XmlElement]
public string ExpiryDate { get; set; } = "";
[XmlIgnore]
public string IgnoreMe { get; set; } // This will be ignored
}
I have BIObject class
public class BIObject
{
public string Database { get; set; }
public string Schema { get; set; }
public string Name { get; set; }
public string ObjectType { get; set; }
}
and also have below XML
<root>
<objects>
<object database="LynxReporting" schema="dbo" name="rptusp_GLTDRSummary" type="P"/>
<object database="IntraDay" schema="dbo" name="BMHGLCloseDetails" type="T"/>
<object database="LynxReporting" schema="dbo" name="factGeneralLedger" type="P"/>
</objects>
</root>
I need to create List<IBObject> from the XML
Note: I load the XML from database into a property of type SqlXml so I need to convert SqlXml to List<IBObject>
Looked at couple of answers but could not figure out how I can do that.
EDIT:
I used below code
using (StringReader reader = new StringReader(myXmlString))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<BIObject>));
List<BIObject> objs = (List<BIObject>)serializer.Deserialize(reader);
}
but got error
There is an error in XML document (1, 2).
and
root xmlns='' was not expected.
Create a class which "represent" your xml structure
[XmlRoot("root")]
public class BIObjects
{
public BIObjects()
{
Objects = new List<BIObject>();
}
[XmlArray("objects")]
[XmlArrayItem("object")]
public List<BIObject> Objects { get; set; }
}
public class BIObject
{
[XmlAttribute("database")]
public string Database { get; set; }
[XmlAttribute("schema")]
public string Schema { get; set; }
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("type")]
public string ObjectType { get; set; }
}
Then use same serializer code you provide in the question
using (StringReader reader = new StringReader(myXmlString))
{
XmlSerializer serializer = new XmlSerializer(typeof(BIObjects));
var objs = (BIObjects)serializer.Deserialize(reader);
// use results
// foreach(BIObject obj in objs.Objects)
}
I am having problems deserializing XML to an object with a list. The deserialize runs without error but the Resources list is empty, and I know there is one resource returned in the XML. It seems like it is just not being deserialised properly.
I am using the following code to deserialize
var ser = new XmlSerializer(typeof(SearchResult));
var test = result.Content.ReadAsStringAsync();
var t = (SearchResult)ser.Deserialize(result.Content.ReadAsStreamAsync().Result);
The variable "test" on line 2 of the code above is equal to
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ns3:searchResult total="1" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns3="ers.ise.cisco.com">
<resources>
<resource id="76a4b0f2-64e2-11e6-9f15-00505688a404" name="5555884552">
<link rel="self" href="https://servername123:9060/ers/config/guestuser/76a4b0f2-64e2-11e6-9f15-00505688a404" type="application/xml"/>
</resource>
</resources>
</ns3:searchResult>
So I would expect to get one ExistingWifiAccountDto in the Resources list. But I don't. It is empty. What am I doing wrong?
The classes for object mapping are below
[XmlRoot(ElementName = "searchResult", Namespace = "ers.ise.cisco.com")]
public class SearchResult
{
public SearchResult()
{
Resources = new List<ExistingWifiAccountDto>();
}
[XmlArray("resources")]
[XmlArrayItem("resource", typeof(ExistingWifiAccountDto))]
public List<ExistingWifiAccountDto> Resources { get; set; }
}
public class ExistingWifiAccountDto
{
public ExistingWifiAccountDto()
{
}
[XmlAttribute("id")]
public string Id { get; set; }
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("link")]
public LinkDto Link { get; set; }
}
public class LinkDto
{
public LinkDto()
{
}
[XmlAttribute("rel")]
public string Rel { get; set; }
[XmlAttribute("href")]
public string Href { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
}
You'll have to set the namespace to an empty string for the "resources" array since it is not inherited in you situation. This should then flow down to child elements.
Try changing
[XmlArray("resources")]
to
[XmlArray("resources", Namespace = "")]
This will cause it to deserialize correctly, alternatively, you could also set each node from "resources" down with the Form attribute:
Form = XmlSchemaForm.Unqualified
Cheers
I have figured out how to populate a custom class from XML data, but I ran into an issue along the way. Things were working perfectly with my existing method of populating data until I was thrown a bit of a curve ball. The new schema I was sent is similar to this:
<ITEM_REPLY>
<TRAN_ID>1320691307345</TRAN_ID>
<REPLY_CODE>0</REPLY_CODE>
<UNIT_PRICE>8.2784</UNIT_PRICE>
<SUP_LOCS>
<SUP_LOC>
<SUP_LOC_ID>001134</SUP_LOC_ID>
<COUNTRY_ID>USA</COUNTRY_ID>
<QTY_AVL>47.000</QTY_AVL>
<ITEM_UOM>EA</ITEM_UOM>
</SUP_LOC>
<SUP_LOC>
<SUP_LOC_ID>006817</SUP_LOC_ID>
<COUNTRY_ID>USA</COUNTRY_ID>
<QTY_AVL>20.000</QTY_AVL>
<ITEM_UOM>EA</ITEM_UOM>
</SUP_LOC>
</SUP_LOCS>
<MESSAGE />
<QTY_BREAKS />
</ITEM_REPLY>
Pretty standard XML schema, problem is I'm not sure how to populate my custom class with it. Here's what I do have:
static void Main(string[] args)
{
var order = ConvertXMLMessage<ItemReply>(request);
}
protected static T ConvertXMLMessage<T>(String xmlData) where T : class, new()
{
var xml = new XmlDocument();
xml.LoadXml(xmlData);
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (var xmlReader = new XmlNodeReader(xml.DocumentElement))
{
T work = (T)(serializer.Deserialize(xmlReader));
return work;
}
}
public class ItemReply
{
[XmlElement("ITEM_REPLY")]
public ItemAvlReply ITEM_REPLY { get; set; }
}
public class ItemAvlReply
{
[XmlElement("TRAN_ID")]
public string TRAN_ID { get; set; }
[XmlElement("REPLY_CODE")]
public string REPLY_CODE { get; set; }
[XmlElement("UNIT_PRICE")]
public string UNIT_PRICE { get; set; }
[XmlElement("SUP_LOCS")]
public SupplierLocations SUP_LOCS;
[XmlElement("MESSAGE")]
public string MESSAGE { get; set; }
[XmlElement("QTY_BREAKS")]
public string QTY_BREAKS { get; set; }
}
public class SupplierLocations
{
[XmlElement("SUP_LOC")]
public List<SupplierLocation> SUP_LOC;
}
public class SupplierLocation
{
[XmlElement("SUP_LOC_ID")]
public string SUP_LOC_ID { get; set; }
[XmlElement("COUNTRY_ID")]
public string COUNTRY_ID { get; set; }
[XmlElement("QTY_AVL")]
public string QTY_AVL { get; set; }
[XmlElement("ITEM_UOM")]
public string ITEM_UOM { get; set; }
}
This works perfectly minus the List<Item> part. I'm not overly experienced with LINQ and I'm not sure how to go about declaring a sub array in my class via this statement. I am also open to a different approach from creating the List<Item> part, I'm just not sure where to start otherwise. Is there a better approach for what I'm need to do? Is there an easy solution I am just unaware of in LINQ?
Here's a simple way to do it, assuming the example XML file you provided has typos. I assumed the OrderId has a closing tag, and that the closing tag for Items should be /Items.
Here's the version of the xml I used:
<Order>
<TransactionID>123</TransactionID>
<OrderID>1</OrderID>
<Items Number="2">
<Item>
<ItemName>Test</ItemName>
<Color>Red</Color>
</Item>
<Item>
<ItemName>Test1</ItemName>
<Color>Blue</Color>
</Item>
</Items>
</Order>
Here's the code to read/write the XML: (the xml variable is a String)
var order = ConvertXMLMessage<Order>(xml);
WriteXMLFile<Order>(order, #"test.xml");
Here's the ConvertXMLMessage and WriteXMLFile functions:
protected static T ConvertXMLMessage<T>(String xmlData) where T : class, new()
{
var xml = new XmlDocument();
xml.LoadXml(xmlData);
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (var xmlReader = new XmlNodeReader(xml.DocumentElement))
{
T work = (T)(serializer.Deserialize(xmlReader));
return work;
}
}
protected static void WriteXMLFile<T>(T item, String saveLocation) where T : class, new()
{
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.IO.StreamWriter file = new System.IO.StreamWriter(saveLocation);
writer.Serialize(file, item);
file.Close();
}
and here's the class structure:
public class Order
{
[XmlElement("TransactionID")]
public string TransactionId { get; set; }
[XmlElement("OrderID")]
public string OrderId { get; set; }
[XmlElement("Items")]
public ItemsContainer Items;
}
public class ItemsContainer
{
[XmlAttribute("Number")]
public Int32 Number { get; set; }
[XmlElement("Item")]
public List<Item> Items;
}
public class Item
{
[XmlElement("ItemName")]
public string ItemName { get; set; }
[XmlElement("Color")]
public string Color { get; set; }
}
As you'll notice I added some attributes to let the XML parser know how to handle the class when it's converting from/to the XML. I also added another small class called "ItemsContainer" just to hold the details on the Items tag. If you didn't need the "Number" attribute, then you could probably find a way to do away with this. However, this should get you in the right direction.
The example I provided is a simple version of how I usually handle the situation, obviously there's some improvements you can make depending on your needs.
Edit
I changed the Item class to use ItemName instead of TransactionId. It was an oversight on my part.
Edit 2
Here's the corrections you need to make to the newly posted code. The reason the Order class worked in the previous example was it matched the root XML element. You're new XML does align with the base class. So we need to add in a couple more attributes to make this work. You can also remove your ItemReply class. It's not needed.
So here's the new classes:
[XmlRoot("ITEM_REPLY")]
public class ItemAvlReply
{
[XmlElement("TRAN_ID")]
public string TRAN_ID { get; set; }
[XmlElement("REPLY_CODE")]
public string REPLY_CODE { get; set; }
[XmlElement("UNIT_PRICE")]
public string UNIT_PRICE { get; set; }
[XmlElement("SUP_LOCS")]
public SupplierLocations SUP_LOCS;
[XmlElement("MESSAGE")]
public string MESSAGE { get; set; }
[XmlElement("QTY_BREAKS")]
public string QTY_BREAKS { get; set; }
}
public class SupplierLocations
{
[XmlElement("SUP_LOC")]
public List<SupplierLocation> SUP_LOC;
}
public class SupplierLocation
{
[XmlElement("SUP_LOC_ID")]
public string SUP_LOC_ID { get; set; }
[XmlElement("COUNTRY_ID")]
public string COUNTRY_ID { get; set; }
[XmlElement("QTY_AVL")]
public string QTY_AVL { get; set; }
[XmlElement("ITEM_UOM")]
public string ITEM_UOM { get; set; }
}
Everything else should remain the same. The parsing/converting the XML to classes should work without any changes.