2017-11-10 1 views
1

Ich habe eine Klasse mit XMLRoot und ein XMLElement für Array. Basierend auf dem Client, wenn ich meine Klasse serialisieren muss, muss ich XMLRoot und XMLElement ändern. Gibt es eine Möglichkeit, es dynamischXMLRoot-Element dynamisch einfügen oder aktualisieren

[XmlRoot("sample")] 
public class MyData 
{ 
    private ArrayList map; 

    public MyData() 
    { 
     map = new ArrayList(); 
    } 

    [XmlElement("url")] 
    public Location[] Locations 
    { 
     get 
     { 
      Location[] items = new Location[map.Count]; 
      map.CopyTo(items); 
      return items; 
     } 

     set 
     { 
      if (value == null) 
       return; 
      Location[] items = (Location[])value; 
      map.Clear(); 
      foreach (Location item in items) 
       map.Add(item); 
     } 
    } 

    public int Add(Location item) 
    { 
     return map.Add(item); 
    } 
} 

zu ändern Wie Sie sehen können, meine Wurzel ist „Probe“, basierend auf Client kann es „Probe“ oder „reserviert“ sein. XMLElement ist "url" und basiert auf Client kann es sein, "url" oder "Dataitem"

Ich Serialisierung XMLSerializer mit

// My condition needs to be here to determine which 
// root and xmlelement should use 
var xs = new XmlSerializer(typeof(MyData)); 
var oString = new StringWriterWithEncoding(Encoding.UTF8); 

Vielen Dank im Voraus

Antwort

1

Siehe MSDN-Overrides on XmlSerializer

Sie kann Überschreibungen über den Konstruktor liefern - am besten lesen Sie die Doku.

Beispiel:

using System.Collections; 
using System.Linq; 
using System.Xml; 
using System.Xml.Serialization; 

public class Location 
{ 
    public string L; 
} 

[XmlRoot("sample")] 
public class MyData 
{ 
    public MyData() 
    { 
     map = new ArrayList(); 
    } 

    [XmlElement("url")] 
    public Location[] Locations 
    { 
     get 
     { 
      Location[] items = new Location[map.Count]; 
      map.CopyTo(items); 
      return items; 
     } 

     set 
     { 
      if (value == null) 
       return; 
      Location[] items = (Location[])value; 
      map.Clear(); 
      foreach (Location item in items) 
       map.Add(item); 
     } 
    } 

    public int Add(Location item) 
    { 
     return map.Add(item); 
    } 

    private ArrayList map; 
} 

internal class Program 
{ 

XML Siebdruck:

private static void DoSerialize(MyData m, XmlSerializer xs) 
    { 
     var settings = new XmlWriterSettings(); 
     settings.OmitXmlDeclaration = true; 
     settings.Indent = true; 
     settings.NewLineOnAttributes = true; 

     var sww = new System.IO.StringWriter(); 
     XmlWriter writer = XmlWriter.Create(sww, settings); 
     xs.Serialize(writer, m); 
     Console.WriteLine(sww.ToString().Replace("><", ">\r\n<")); 
    } 

Verwendung von überladenen Konstruktor:

static void Main() 
    { 
     // testdata 
     MyData m = new MyData 
     { 
      Locations = new Location[2] 
      { 
       new Location { L = "L1" }, 
       new Location { L = "L2" } 
      } 
     }; 

     // simple Serializer 
     var xs = new XmlSerializer(typeof(MyData)); 

     DoSerialize(m, xs); 
     Console.WriteLine(); 


     var xs2 = new XmlSerializer(typeof(MyData), XmlAttributeOverride(), 
        new Type[] { typeof(Location[]) }, RootOverride(), ""); 

     DoSerialize(m, xs2); 

     Console.ReadLine(); 
    } 

    // override the root node 
    private static XmlRootAttribute RootOverride() => new XmlRootAttribute("OtherName"); 

    // override your Languages property 
    private static XmlAttributeOverrides XmlAttributeOverride() 
    { 
     var attrs = new XmlAttributes(); 
     attrs.XmlElements.Add(new XmlElementAttribute("Location")); 

     var o = new XmlAttributeOverrides(); 
     o.Add(typeof(MyData), "Locations", attrs); 
     return o; 
    } 
} 

Ausgang:

<sample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:// 
www.w3.org/2001/XMLSchema"> 
    <url> 
    <L>L1</L> 
    </url> 
    <url> 
    <L>L2</L> 
    </url> 
</sample> 

<OtherName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http 
://www.w3.org/2001/XMLSchema"> 
    <Location> 
    <L>L1</L> 
    </Location> 
    <Location> 
    <L>L2</L> 
    </Location> 
</OtherName> 
+0

Es funktioniert fast wie pro Ihre Lösung. Einziges Ding, das ich jetzt ausstelle, ist, dass ich xmlns Deklaration an oder level bekomme, die ich – jvm

+0

nicht will, erstellen Sie eine neue SO Frage und stellen Sie Details zur Verfügung. Ich freue mich über eine positive Antwort oder eine angenommene Antwort. –

+0

Ich bekomme https: //www.domaincom/url .html immer 2017-09-11T04: 50: 27Z jvm

Verwandte Themen