简体   繁体   中英

Convert XmlSerializer code from java to C#

I have a Java Android application that seralizes a document correctly, but I am required to build an equivalent using ASP.NET/C#.

XMLSerializer in Java and XmlSerializer in C# appear to be completely different animals. The Java version seems to have loads of methods not found in the C# version.

Here is an example of the code I have:

    XmlSerializer serializer = Xml.newSerializer();
    StringWriter writer = new StringWriter();
    serializer.setOutput(writer); *
    serializer.startDocument("UTF-8", true); *
    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); *
    serializer.startTag(null, "root"); *
    serializer.flush(); *
    writer.write(value);
    serializer.endTag(null, String.valueOf(type).toLowerCase()); *
    serializer.startTag(null, "image");
    serializer.text(Base64.encodeToString(image, Base64.DEFAULT)); *
    serializer.endTag(null, "image");
    serializer.endDocument(); *

None of the methods listed here (marked with asterix) are available in C#. How can I possibly emulate it? I have searched the net and can't find any decent documentation on the subject. Does anyone know what the C# equivalents would be for the methods shown above?

  • System.Xml.XmlWriter.Flush <-> org.xmlpull.v1.XmlSerializer.flush
  • System.Xml.XmlWriter.Create(Stream) <-> org.xmlpull.v1.XmlSerializer.setOutput(Writer)
  • System.Xml.XmlWriter.WriteStartDocument <-> org.xmlpull.v1.XmlSerializer.startDocument
  • System.Xml.XmlWriter.WriteEndDocument <-> org.xmlpull.v1.XmlSerializer.endDocument
  • System.Xml.XmlWriter.WriteStartElement <-> org.xmlpull.v1.XmlSerializer.startTag
  • System.Xml.XmlWriter.WriteEndElement <-> org.xmlpull.v1.XmlSerializer.endTag
  • System.Xml.XmlWriter.WriteString <-> org.xmlpull.v1.XmlSerializer.text
  • System.Xml.XmlWriter.WriteBase64 <-> your image
  • System.Xml.XmlWriter.WriteProcessingInstruction <-> org.xmlpull.v1.XmlSerializer.setFeature

References on msdn:

  1. XmlWriter , forward-only xml stream generator
  2. XmlWriterSettings
  3. XmlWriter encoding
  4. Namespace Handling in the XmlWriter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM