I have to generate (serialize to) XML from object with array of (Order) elements.
Order class generated from XSD has sequence attribute:
[System.Xml.Serialization.XmlAttributeAttribute(DataType = "token")]
public string Sequence;
I am using .Net XMLSerializer, but it does not generate automagically for every Order element Sequence attribute.
Having:
Order[] Orders = new Order[2] {...}
I have to get:
<Order Sequence="1">..</Order>
<Order Sequence="2">..</Order>
And for just one, single element it should render:
<Order Sequence="1">..</Order>
Does anyone know how to make XMLSerialzier renders this attribute automagically? Or do I need to manually set Sequence for every Order element?
Cheers
AFAIK there is no way to achieve this with out-of-the-box methods. This leaves you with the following options:
IXmlSerializable
Implement IXmlSerializable on the object that contains the array of Orders. This way, you can either serialize the Orders manually or set the sequence number of the Order before serializing the object into the XmlWriter using the XmlSerializer.Serialize method:
public class OrdersContainer : IXmlSerializable
{
public Order[] Orders;
public void WriteXml(XmlWriter writer)
{
// Serialize other properties
writer.WriteStartElement("Orders");
var ser = new XmlSerializer(typeof(Order));
for(var i = 0; i < Orders.Length; i++)
{
Orders[i].Sequence = (i + 1).ToString();
ser.Serialize(writer, Orders[i]);
}
writer.WriteEndElement(); // Orders
}
// ...
}
This generated the following XML:
<?xml version="1.0" encoding="utf-16"?>
<OrdersContainer>
<Orders>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Sequence="1">
<Name>Order 1</Name>
</Order>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Sequence="2">
<Name>Order 2</Name>
</Order>
</Orders>
</OrdersContainer>
The XmlSerializer places some namespace declarations on the Order elements, but that doesn't hurt. My sample class had a Name property on the Order class.
The downside of this approach is that you have to implement the serialization of the OrdersContainer class manually, including the deserialization.
Linq to XML
The second option is to use Linq to XML for the serialization part. You'd also have to implement the serialization manually, but you could use the otb XmlSerializer for deserialization (on the other hand you might want to avoid to mix two different frameworks). You could serialize the OrdersContainer class as follows and also write the sequence number:
var doc = new XDocument(new XElement("OrdersContainer",
new XElement("Orders",
cont.Orders.Select((x, i) => new XElement("Order",
new XAttribute("Sequence", (i + 1).ToString()),
new XElement("Name", x.Name))))));
doc.Save(writer);
This created the following XML:
<?xml version="1.0" encoding="utf-16"?>
<OrdersContainer>
<Orders>
<Order Sequence="1">
<Name>Order 1</Name>
</Order>
<Order Sequence="2">
<Name>Order 2</Name>
</Order>
</Orders>
</OrdersContainer>
Please note that the sample uses an overload of the Select method that also receives the index of the item so that the sequence number can be created based upon the position in the array.
If order is a class of yours, you can add a property with [XmlAttributeAttribute]
class Order {
[XmlAttribute()]
public int Sequence { get; set; }
But you should set this Sequence property in all items of your list.
this works fine -
var o = new XmlSerializerNamespaces();
o.Add("", "");
var ser = new XmlSerializer(typeof(List<Order>), "");
using (var sw = new StringWriter())
{
ser.Serialize(sw, new List<Order> {
new Order { sequence = "1", MyProperty = 1 },
new Order { sequence = "2", MyProperty = 2 } },
o);
var t = sw.ToString();
}
AND
[XmlAttribute(AttributeName = "sequence", DataType = "string")]
public string sequence { get; set; }
Related
I have generated an xml by using DataTable.WriteXML() method, which converts my table fields into elements of an xml file - that's precisely what I need.
Now I want to add another element and make an existing element as its child.
Existing:
<DocketImport>
<Docket>
<XRefCode>1578</XRefCode>
<Name>1578</Name>
<PieceRate>0</PieceRate>
<OrgXRefCode>terminalA</OrgXRefCode>
</Docket>
</DocketImport>
Desired:
<DocketImport>
<Docket>
<XRefCode>1578</XRefCode>
<Name>1578</Name>
<PieceRate>0</PieceRate>
<Org>
<OrgXRefCode>terminalA</OrgXRefCode>
</Org>
</Docket>
</DocketImport>
In case following result would be acceptable:
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<DataTable1>
<XRefCode>1578</XRefCode>
<Name>1578</Name>
<PieceRate>0</PieceRate>
<Org xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OrgXRefCode>terminalA</OrgXRefCode>
</Org>
</DataTable1>
</DocumentElement>
You can achieve desired behavior by defining custom type for last columns of your DataTable
public class Org
{
public string OrgXRefCode { get; set; }
}
Than your DataTable initialization could look as following:
var dataTable = new DataTable("DataTable2");
dataTable.Columns.AddRange(new DataColumn[]{
new DataColumn("XRefCode"),
new DataColumn("Name"),
new DataColumn("PieceRate"),
new DataColumn("Org", typeof(Org))
});
var dataRow = dataTable.NewRow();
dataRow["XRefCode"] = 1578;
dataRow["Name"] = 1578;
dataRow["PieceRate"] = 0;
dataRow["Org"] = new Org()
{
OrgXRefCode = "terminalA"
};
dataTable.Rows.Add(dataRow);
calling dataTable.WriteXml() will produce your expected XML
I am trying to create an XML document that closely conforms to a C# object graph and its JSON representation, but am having difficulty with the list representation in the XML. Given this graph
public class X
{
public List<A> Aa { get; set; }
}
public class A
{
public int B;
public bool C;
}
I took the JSON from the above, and tried converting it a couple of ways:
var json = #"{""Aa"":[{""B"":186,""C"":true},{""B"":9,""C"":false},{""B"":182,""C"":true}]}";
var xml = JsonConvert.DeserializeXNode(json, typeof(T).Name, false);
var xml2 = JsonToXml(json);
This produced the following for xml (no Aa "container node"):
<X>
<Aa><B>186</B><C>true</C></Aa>
<Aa><B>9</B><C>false</C></Aa>
<Aa><B>182</B><C>true</C></Aa>
</X>
And for xml2 (has "container" node, but some extra noise):
<root type="object">
<Aa type="array">
<item type="object">
<B type="number">186</B>
<C type="boolean">true</C>
</item>
<item type="object">
<B type="number">9</B>
<C type="boolean">false</C>
</item>
<item type="object">
<B type="number">182</B>
<C type="boolean">true</C>
</item>
</Aa>
</root>
The method used to produce the value for xml2 comes from a different approach using the .NET Framework:
XDocument JsonToXml(string jsonString)
{
using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(jsonString)))
{
var quotas = new XmlDictionaryReaderQuotas();
return XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(stream, quotas));
}
}
What I want to produce is
<X>
<Aa>
<A><B>186</B><C>true</C></A>
<A><B>9</B><C>false</C></A>
<A><B>182</B><C>true</C></A>
</Aa>
</X>
I have tried changing the writeArrayAttribute parameter of DeserializeXDocument to true, but that doesn't work either. The documentation for converting between JSON and XML does not help.
How can I produce the compact version that contains the items in a parent Aa node? Is this going to require some custom deserializer?
The original JSON was created via
var json = JsonConvert.SerializeObject(new X { etc }, Formatting.None, settings);
The Problem.
Your difficulty arises because there are two common ways to serialize a collection to XML, and Json.NET only supports automatic JSON-to-XML conversion for one of them.
Specifically, when serializing a c# collection to XML (with, say, XmlSerializer), the collection can be serialized either with, or without, an outer container element. The former looks like the following:
<X>
<Aa>
<A>
<B>186</B>
<C>true</C>
</A>
<A>
<B>9</B>
<C>false</C>
</A>
</Aa>
</X>
While the latter looks like:
<X>
<Aa>
<B>186</B>
<C>true</C>
</Aa>
<Aa>
<B>9</B>
<C>false</C>
</Aa>
</X>
When Json.NET converts a JSON array to XML elements, it uses the second format for the array, since the JSON only contains one property name while the two-level XML format requires both inner and outer element names. I.e. in your JSON:
{"Aa":[{"B":186,"C":true},{"B":9,"C":false}]}
Only the name "Aa" appears. The name "A" never does, so DeserializeXNode() cannot know to insert it. This makes the second format the straightforward choice for canonical conversion, whereas you require the first.
The Solution.
To generate a two-level XML collection from a JSON array, you'll need to either insert synthetic JSON objects before conversion, or synthetic XML elements afterwards. For the former, this can be done by parsing the JSON string to an intermediate JToken, and modifying it as follows:
var jObject = JObject.Parse(json);
jObject.SelectTokens("Aa").WrapWithObjects("A");
var finalXml = jObject.ToXElement(typeof(X).Name, false);
Using the extension methods:
public static class JsonExtensions
{
public static void WrapWithObjects(this IEnumerable<JToken> values, string name)
{
foreach (var value in values.ToList())
{
var newParent = new JObject();
if (value.Parent != null)
value.Replace(newParent);
newParent[name] = value;
}
}
public static XElement ToXElement(this JObject obj, string deserializeRootElementName = null, bool writeArrayAttribute = false)
{
if (obj == null)
return null;
using (var reader = obj.CreateReader())
return JsonExtensions.DeserializeXElement(reader, deserializeRootElementName, writeArrayAttribute);
}
static XElement DeserializeXElement(JsonReader reader, string deserializeRootElementName, bool writeArrayAttribute)
{
var converter = new Newtonsoft.Json.Converters.XmlNodeConverter() { DeserializeRootElementName = deserializeRootElementName, WriteArrayAttribute = writeArrayAttribute };
var jsonSerializer = JsonSerializer.CreateDefault(new JsonSerializerSettings { Converters = new JsonConverter[] { converter } });
return jsonSerializer.Deserialize<XElement>(reader);
}
}
Alternatively, you could tell XmlSerializer to (de)serialize the Aa list without a container element by marking it with [XmlElement]:
public class X
{
[XmlElement]
public List<A> Aa { get; set; }
}
Now the xml generated by JsonConvert.DeserializeXNode will be deserializable directly.
I've been trying to find a good clean way to load the contents of an XML file into an array to use but I've only found partial answers here and there. My XML file is an Embedded Resource for simplicity, and contains a list of about 115 elements that all contain an id and name attribute.
The XML looks like so:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Item>
<id>1</id>
<name>Example1</name>
</Item>
<Item>
<id>2</id>
<name>Example2</name>
</Item>
<Item>
<id>3</id>
<name>Example3</name>
</Item>
</Items>
I'm able to load everything in and I see my data in the InnerXML but I cannot find out how to access it correctly.
public Form1()
{
InitializeComponent();
assembly = Assembly.GetExecutingAssembly();
XmlDocument xml = null;
try
{
string filePath = "MyProject.ItemList.xml";
Stream fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filePath);
if (fileStream != null)
{
xml = new XmlDocument();
xml.Load(fileStream);
}
}
catch {
//Do nothing
}
XmlDocument itemsFromXML = xml.DocumentElement.InnerXml;
foreach (XmlNode node in itemsFromXML)
{
int id = Convert.ToInt32(node.Attributes.GetNamedItem("id").ToString());
string name = node.Attributes.GetNamedItem("name").ToString();
gameItemList.Add(new GameItem(id, name));
}
}
That's the code I have that would ideally set this array up for me to use, though it is fairly broken due to me trying different things, but I think it conveys the general idea. Hopefully someone can make some sense of it and explain to me what I'm doing horribly wrong (>.<) I would be happy to provide more information, clarification, etc if I missed something important!
Thanks!
Using System.Xml.Linq:
var items = XElement.Load(fileStream)
.Elements("Item")
.Select(itemXml => new {
id = (int)itemXml.Element("id").Value,
name = itemXml.Element("name").Value
})
.ToArray();
Use an xpath.
XmlNodeList nodes = xml.SelectNodes("Items/Item");
foreach ( XmlNode node in nodes )
{
int id = int.Parse(node.SelectSingleNode("id").InnerText);
}
Minoccurs is 0 in the XSD and nillable is true for an element.
But if I don't set the element value, it takes it as null and the record is blanked out on the server. Is there a way to tell it to omit the element from the output XML when some conditions are satisfied but have it for other cases?
<xs:element name='CLS_CD' minOccurs='0' nillable='true' type='xdv:stringLen20'/>
If you are using XmlSerializer, you can control whether the value is emitted by including a PropertyNameSpecified property.
Another option is to use a special
pattern to create a Boolean field
recognized by the XmlSerializer, and
to apply the XmlIgnoreAttribute to the
field. The pattern is created in the
form of propertyNameSpecified. For
example, if there is a field named
"MyFirstName" you would also create a
field named "MyFirstNameSpecified"
that instructs the XmlSerializer
whether to generate the XML element
named "MyFirstName".
For example, if you declare the class like this:
public class Data
{
[XmlIgnore]
public bool CLS_CDSpecified { get; set; }
[XmlElement(IsNullable=true)]
public string CLS_CD { get; set; }
}
Then you can serialize nothing, an explicit nil value, or an actual value:
var serializer = new XmlSerializer(typeof(Data));
var serializesNothing = new Data();
serializesNothing.CLS_CD = null;
serializesNothing.CLS_CDSpecified = false;
serializer.Serialize(Console.Out, serializesNothing);
Console.WriteLine();
Console.WriteLine();
var serializesNil = new Data();
serializesNil.CLS_CD = null;
serializesNil.CLS_CDSpecified = true;
serializer.Serialize(Console.Out, serializesNil);
Console.WriteLine();
Console.WriteLine();
var serializesValue = new Data();
serializesValue.CLS_CD = "value";
serializesValue.CLS_CDSpecified = true;
serializer.Serialize(Console.Out, serializesValue);
Output:
<?xml version="1.0" encoding="IBM437"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
<?xml version="1.0" encoding="IBM437"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CLS_CD xsi:nil="true" />
</Data>
<?xml version="1.0" encoding="IBM437"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CLS_CD>value</CLS_CD>
</Data>
I'm trying to deserialize the following XML output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Foo>
<Val>Data1</Val>
</Foo>
<Foo>
<Val>Data2</Val>
</Foo>
(This is output from a hardware device, and cannot be changed)
I have an XML type defined as:
[XmlType(AnonymousType=true, Namespace="")]
public class Foo
{
public string Val { get; set; }
}
I've tried to deserialize this array by creating a serializer like:
var s = new XmlSerializer(typeof(Foo[]));
//or
var s = new XmlSerializer(typeof(List<Foo>);
But every call to s.Deserialize() causes an InvalidOperaitonException:
System.InvalidOperationException: <Foo xmlns=''> was not expected.
Note
var s = new XmlSerializer(typeof(Foo));
// Only deseralizes the first Foo (Data1).
Thanks for your help.
I think the issue is with your provided xml.
Test app says
List<Foo> list = new List<Foo> {new Foo {Val = "Data1"}, new Foo {Val = "Data2"}};
var s = new XmlSerializer(typeof(List<Foo>));
StringBuilder sb = new StringBuilder();
XmlWriter wr = XmlWriter.Create(sb);
s.Serialize(wr, list);
string ss = sb.ToString();
var s2 = new XmlSerializer(typeof(List<Foo>));
StringReader sr = new StringReader(ss);
List<Foo> returnList = (List<Foo>)s2.Deserialize(sr);
And the XML should be
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfFoo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Foo>
<Val>Data1</Val>
</Foo>
<Foo>
<Val>Data2</Val>
</Foo>
</ArrayOfFoo>
If you can remove the inital line
<?xml version="1.0" encoding="ISO-8859-1"?>
And minipulate the string into
string s = "<ArrayOfFoo><Foo> <Val>Data1</Val></Foo><Foo> <Val>Data2</Val></Foo></ArrayOfFoo>";
var s2 = new XmlSerializer(typeof(List<Foo>));
StringReader sr = new StringReader(s);
List<Foo> list = (List<Foo>)s2.Deserialize(sr);
That could work.
That isn't valid Xml. There needs to be a core root element for it to work properly.
this is not a valid xml so you can not deserialize it like a valid xml. You need some kind of hack to make this work. i'd suggest to insert at beginning of the xml and inserting at the end of xml. then you can deserialize it, since you cant make this change at xml side, do it in your code.
String ss;
// lets assume this holds your xml data in string.
ss.append("</ArrayOfFoo>");
ss.replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "<?xml version=\"1.0\" encoding=\"utf-16\"?> <ArrayOfFoo>")
var s2 = new XmlSerializer(typeof(List<Foo>));
StringReader sr = new StringReader(ss);
List<Foo> returnList = (List<Foo>)s2.Deserialize(sr);
now this shall return you the correct list.
As the other posters say, this XML that the hardware device produces is not compatible to the way .NET serializes/deserializes object. Valid XML, and .NET requires valid XML, has a root element.
I suggest:
either you modify your obtained XML to match the way astander presents in his xml code snippet.
or you write a simple xml parser for your file that deserializes the file like you need
br, Marcel
Technically, what you have there is not a well-formed XML document (which has exactly one root element), but rather an well-formed external parsed entity (which can have any number of elements not contained in other elements, as well as text not contained in any elements). Therefore, it should parse if your XML parser has an entry point for parsing EPEs rather than documents. You could also create a stub document which includes by reference your EPE and parse that document.
Xstream for .Net could be a useful API