I wasn't very specific in my last post so an example may help:

For the data:
<MyObject>
   <Std>
      <MyVal>12</MyVal>
      <MyString>Hello</MyString>
   </Std>
</MyObject>

This code should read/write it(Although I've not actually run it)
(The new that takes filename is to show how to get your reader online)

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

public class MyObject : IXmlSerializable
{
 private int myVal;
 private string myString;
 public MyObject(string fileName)
 { FileStream aReader = new FileStream(fileName,
FileMode.Open);
        XmlTextReader aXmlTextReader = new XmlTextReader(aReader);
        ReadXml(aXmlTextReader);
 }
 public MyObject(XmlReader reader)
 { ReadXml (reader);
 }
 public XmlSchema GetSchema()
 { return null;
 }

 public void ReadXml(XmlReader reader)
 { reader.ReadStartElement ("MyObject");
  // works like a stack, where you push and pop though levels
  reader.ReadStartElement("std");
  myVal = int.Parse( reader.ReadElementString ("MyValue"));
  myString = reader.ReadElementString("MyString");
  reader.ReadEndElement();
  reader.ReadEndElement();
 }

 public void WriteXml(XmlWriter writer)
 { writer.WriteStartElement ("MyObject");
  // Now inside MyObject
  writer.WriteStartElement ("std");
  // Now inside MyObject/std
  writer.WriteElementString ("MyValue", myVal.ToString());
  writer.WriteElementString("MyString",myString);
  writer.WriteEndElement(); // Close MyObject tag
  writer.WriteEndElement(); // Close MyObject tag
 }
}

Any good to you?

Jeremy


>Have you tried implemententing System.Xml.Serialization.IXmlSerializable
>
>You can then serialise and deserialise in whatever way you want.
>Return null or nothing from GetSchema(), and you're away.
>
>You now have the two functions ReadXml and WriteXml to play with
>which recieve System.Xml.XmlReader and System.Xml.XmlWriter
>respectively.
>
>The you can read and write in pretty much anyway you want,
>is that the kind of thing you are after?
>
>
>Jeremy
>
>>>I understand fairly well the use of the [Serializable] attribute
>>>and the native Binary
>>>IFormatter interface. I understand also that there is XML and SOAP
>>>serialization. But all of these seem to serialize to the CLR's view of
>>>an object or something with XML Schema types. What I actually have to do
>>>is: we have some XML files that we create in our application. They were
>>>defined before XML schema and they are not Schema compliant. I have some
>>>.NET classes I am defining and I need to put the data/state in them
>>>written out to these XML files in a certain format. The trick is I can't
>>>just dump .NET's "view" of the objects. The XML files have a certain
>>>format. Can I make use of any of XML Serialization or do I just have
>>>"pound" XML into the files using classes in System.Xml? In other words,
>>>how customizable is the XML serialization? Any ideas would be greatly
>>>appreciated.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to