0

I have a class named Node and inside that i have Property of Type Document Class.

When I serialize it into XML, I get the output as

<Node>
<DocumentType>
<File></File>
</DoumentType>
<Node>

But I want the output as

<Node>
<File></File>
<Node>

Object Code

public class Document
{
    [XmlElement(ElementName = "file")]
    public string File { get; set; }
}

public class Node
{
    public Document NodeDocument
    {
        get;
        set;
    }
}

How can I do that using C# xml Serialization?

2
  • DocumentType sounds like it describes the Node Element so why not set it as an attribute instead using [XmlAttribute]. <Node DocumentType="test"> Commented Nov 20, 2012 at 11:02
  • Can you post the objects code? There are alternative document types to File presumably? Is DocumentType an array? Commented Nov 20, 2012 at 11:02

2 Answers 2

1

Following Kami's suggestion, here is the code for your reference. All credit goes to Kami.

public class Node : IXmlSerializable {

    public Node() {
        NodeDocument = new Document();
    }

    public Document NodeDocument { get; set; }

    public System.Xml.Schema.XmlSchema GetSchema() {
        return null;
    }

    public void ReadXml(XmlReader reader) {            
        reader.ReadStartElement();
        NodeDocument.File = reader.ReadString();
        reader.ReadEndElement();
    }

    public void WriteXml(XmlWriter writer) {
        writer.WriteStartElement("file");
        writer.WriteString(NodeDocument.File);
        writer.WriteEndElement();
    }
}

public class Document {
    public String File { get; set; }
}

class Program {
    static void Main(string[] args) {
        var node = new Node();
        node.NodeDocument.File = "bbb.txt";

        Serialize<Node>("a.xml", node);

        node = Deserialize<Node>("a.xml");
        Console.WriteLine(node.NodeDocument.File);

        Console.Read();
    }

    static T Deserialize<T>(String xmlFilePath) where T : class {
        using (var textReader = File.OpenText(xmlFilePath)) {
            using (var xmlTextReader = new XmlTextReader(textReader)) {
                var serializer = new XmlSerializer(typeof(T));
                return (T)serializer.Deserialize(xmlTextReader);
            }
        }
    }

    static void Serialize<T>(String xmlFilePath, T obj) where T : class {
        using (var textWriter = File.CreateText(xmlFilePath)) {
            using (var xmlTextWriter = new XmlTextWriter(textWriter)) {
                var serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(xmlTextWriter, obj);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

But I also have other properties that I need to export to XML. Which are not string. They may be List,boolean, or an another class.
0

Have you considered implementing IXmlSerializable interface - http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx.

You should then be able to write custom serialization/deserialization to facilitate the above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.