XML Serialize Attribute, Elements

  • Thread starter Thread starter Markus Freitag
  • Start date Start date
M

Markus Freitag

Guest
Hello,

My goal is to read in the file according to the customer's specifications and then rewrite it.

Then I have a structure, a data model that I can extend.

1287743.jpg


How to do it right? How can I write attributes instead of elements?
Maybe someone has tips and examples. Thank you in advance.

With best regards Markus

The result is this. All variables are elements.
IsBe


<Orders>
<ListMaterialProperties />
<ListIndexPanels>
<IndexPanel>
<No>1</No>
<Finished>0</Finished>
<ListIndexProperties>
<IndexProperty>
<No>2</No>
<IndexValue>i1</IndexValue>
<Ref1>333</Ref1>
<Ref2>444</Ref2>
<Ref3>555</Ref3>
<State>0</State>
</IndexProperty>
<IndexProperty>
<No>2</No>
<IndexValue>i1</IndexValue>
<Ref1>333</Ref1>
<Ref2>444</Ref2>
<Ref3>555</Ref3>
<State>0</State>
</IndexProperty>
<IndexProperty>

MustBe:
<Orders Type="OWN" Version="1">
<WorkOrder ID="068765411">
<Item Model="203 777-01 V05">
<Quantity>3</Quantity>
<Panel Finished="false">
<Index State="0">
<Ref>0001</Ref>
<Ref>001</Ref>
<Ref>6MIN8F</Ref>
<Value>0687654110001</Value>
</Index>
<Index State="0">
<Ref>0002</Ref>
Save xml with this.

private static readonly XmlWriterSettings WriterSettingsExclusiv = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true, Encoding = new UTF8Encoding(true) };
private static readonly XmlSerializerNamespaces Namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", ""), });
private static readonly XmlReaderSettings ReaderSettings = new XmlReaderSettings { CheckCharacters = false };

public void SaveTraceXML<T>(string file, T dataObject)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));

using (Stream stream = new FileStream(file, FileMode.Create))
using (XmlWriter xmlWriter = XmlWriter.Create(stream, WriterSettingsExclusiv))
{
serializer.Serialize(xmlWriter, dataObject, Namespaces);
}
}

My class
[DataContract]
public class Orders
{
[DataMember(IsRequired = true)]
public string Order { get; set; }

[DataMember(IsRequired = true)]
public List<IndexPanel> ListIndexPanels;

Continue reading...
 
Back
Top