2016-06-01 5 views
1

Ich arbeite an XML-Generierung mit Liste zu Xml der generischen Klasse verweisen, aber falsche Ergebnisse von XML erhalten.Wie erstellt man XML mit Attribut mit XmlSerializer?

Ich möchte unter Ausgabe von XmlSerializer.

<root> 
    <rating city="A" code="A1">A++</rating> 
    <rating city="B" code="A2">A</rating> 
    <rating city="C" code="A3">AB</rating> 
</root> 

derzeit Ich bin dieses immer

<ratings reason="reason123"> 
<rating>R</rating> 
</ratings> 
<ratings reason="reason123"> 
<rating>R</rating></ratings> 

C# -Code

public class root 
{ 
    [XmlAttribute("city")] 
    public string city { get; set; } 
    [XmlAttribute("code")] 
    public string code { get; set; } 
    [XmlElement("rating")] 
    public string rating { get; set; } 
} 
string tempXML6 = XmlExtensions.Serialize(rootList) 

static public void Serialize(object classobject) 
{ 
    XmlSerializer SerializedObject = new XmlSerializer(classobject.GetType()); 
      using (MemoryStream xmlStream = new MemoryStream()) 
      { 
       SerializedObject.Serialize(xmlStream, classobject); 
       xmlStream.Position = 0; 
       //Loads the XML document from the specified string. 
       XmlDocument resultDocument = new XmlDocument(); 
       resultDocument.Load(xmlStream); 
       if (!string.IsNullOrEmpty(resultDocument.DocumentElement.InnerXml)) 
       { 
        return resultDocument.DocumentElement.InnerXml; 
       } 
       else 
       { 
        return string.Empty; 
       } 
      } 
} 

bitte mich wissen lassen, was Veränderung sein muss.

+0

Was die 'classobject' ist serialisiert und ich kann nicht sehen dass Sie den 'objRoot' verwenden –

Antwort

1

Ihr Objektmodell falsch ist. Es sollte so etwas sein.

[XmlRoot("root")] 
public class Root 
{ 
    [XmlElement("rating")] 
    public Rating[] Ratings { get; set; } 
} 

public class Rating 
{ 
    [XmlAttribute("city")] 
    public string City { get; set; } 
    [XmlAttribute("code")] 
    public string Code { get; set; } 
    [XmlText] 
    public string Value { get; set; } 
} 

Um das Objekt tun dies

Root root = new Root 
{ 
    Ratings = new Rating[] 
    { 
     new Rating { City = "A", Code = "A1", Value = "A++" }, 
     new Rating { City = "B", Code = "A2", Value = "A" }, 
     new Rating { City = "C", Code = "A3", Value = "AB" } 
    } 
}; 

string serializedObject = Serialize(root); 

Dies ist die serialize Methode

public static string Serialize<T>(T item) 
{ 
    XmlSerializer serializer = new XmlSerializer(typeof(T)); 
    using (StringWriter stringWriter = new StringWriter()) 
    { 
     using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true })) 
     { 
      serializer.Serialize(xmlWriter, item, new XmlSerializerNamespaces(new[] { new XmlQualifiedName(string.Empty, string.Empty) })); 
     } 

     return stringWriter.ToString(); 
    } 
} 

XML-Ausgabe

<root> 
    <rating city="A" code="A1">A++</rating> 
    <rating city="B" code="A2">A</rating> 
    <rating city="C" code="A3">AB</rating> 
</root> 
+0

t Hanks, perfekt –

0

Bitte ändern Sie Ihre Klasse defination dies zu mögen:

[XmlTypeAttribute(AnonymousType = true)] 
[XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class root 
{ 
    private rootRating[] ratingField; 

    [XmlElementAttribute("rating")] 
    public rootRating[] rating 
    { 
     get 
     { 
      return this.ratingField; 
     } 
     set 
     { 
      this.ratingField = value; 
     } 
    } 
} 

[XmlTypeAttribute(AnonymousType = true)] 
public partial class rootRating 
{ 

    private string cityField; 

    private string codeField; 

    private string valueField; 

    [XmlAttributeAttribute()] 
    public string city 
    { 
     get 
     { 
      return this.cityField; 
     } 
     set 
     { 
      this.cityField = value; 
     } 
    } 

    [XmlAttributeAttribute()] 
    public string code 
    { 
     get 
     { 
      return this.codeField; 
     } 
     set 
     { 
      this.codeField = value; 
     } 
    } 

    [XmlTextAttribute()] 
    public string Value 
    { 
     get 
     { 
      return this.valueField; 
     } 
     set 
     { 
      this.valueField = value; 
     } 
    } 
} 
0
//try this 
    public static XElement SerializeRoot(IEnumerable<root> objRoot) 
    { 

     XElement serializedRoot = null; 

     using (MemoryStream memoryStream = new MemoryStream()) 
     { 

      using (TextWriter StreamWriter = new StreamWriter(memoryStream)) 
      { 

       XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<root>)); 

       xmlSerializer.Serialize(StreamWriter, objRoot); 

       serializedRoot = XElement.Parse(Encoding.UTF8.GetString(memoryStream.ToArray())); 

      } 

     } 

     return serializedRoot; 

    } 
Verwandte Themen