2 // mcs/class/System.Data/System.Xml/XmlDataDocument.cs
4 // Purpose: Provides a W3C XML DOM Document to interact with
5 // relational data in a DataSet
7 // class: XmlDataDocument
8 // assembly: System.Data.dll
9 // namespace: System.Xml
12 // Daniel Morgan <danmorg@sc.rr.com>
13 // Ville Palo <vi64pa@koti.soon.fi>
15 // (c)copyright 2002 Daniel Morgan
17 // XmlDataDocument is included within the Mono Class Library.
24 using System.Xml.XPath;
25 using System.Collections;
27 namespace System.Xml {
29 public class XmlDataDocument : XmlDocument {
33 private DataSet dataSet;
34 private bool isReadOnly = false;
36 private int dataRowID = 1;
37 private ArrayList dataRowIDList = new ArrayList ();
43 public XmlDataDocument() {
44 dataSet = new DataSet();
47 public XmlDataDocument(DataSet dataset) {
48 this.dataSet = dataset;
51 #endregion // Constructors
53 #region Public Properties
55 public override string BaseURI {
58 // TODO: why are we overriding?
63 public DataSet DataSet {
70 // override inheritted method from XmlDocument
71 public override string InnerXml {
74 throw new NotImplementedException();
79 throw new NotImplementedException();
83 public override bool IsReadOnly {
92 public override XmlElement this[string name] {
95 throw new NotImplementedException();
100 public override XmlElement this[string localname, string ns] {
103 throw new NotImplementedException();
107 public override string LocalName {
110 throw new NotImplementedException();
114 public override string Name {
117 throw new NotImplementedException();
121 public override XmlDocument OwnerDocument {
128 #endregion // Public Properties
130 #region Public Methods
133 public override XmlNode CloneNode(bool deep)
135 throw new NotImplementedException();
138 #region overloaded CreateElement methods
141 public new XmlElement CreateElement(string prefix,
142 string localName, string namespaceURI)
144 throw new NotImplementedException();
148 public new XmlElement CreateElement(string qualifiedName,
151 throw new NotImplementedException();
155 public new XmlElement CreateElement(string name)
157 throw new NotImplementedException();
160 #endregion // overloaded CreateElement Methods
162 // will not be supported
163 public override XmlEntityReference CreateEntityReference(string name)
165 throw new NotSupportedException();
168 // will not be supported
169 public override XmlElement GetElementById(string elemId)
171 throw new NotSupportedException();
174 // get the XmlElement associated with the DataRow
175 public XmlElement GetElementFromRow(DataRow r)
177 throw new NotImplementedException();
180 // get the DataRow associated with the XmlElement
182 public DataRow GetRowFromElement(XmlElement e)
184 throw new NotImplementedException();
187 #region overload Load methods
190 public override void Load(Stream inStream) {
195 public override void Load(string filename) {
196 throw new NotImplementedException();
200 public override void Load(TextReader txtReader) {
201 throw new NotImplementedException();
205 public override void Load(XmlReader reader) {
209 // For reading xml to XmlDocument
210 XmlTextReader textReader = new XmlTextReader (
213 if (reader.NodeType != XmlNodeType.Element)
214 reader.MoveToContent ();
216 // TODO: Findout which exception should be throwen
217 if (reader.NodeType != XmlNodeType.Element)
218 throw new Exception ();
220 if (dataSet.DataSetName != reader.Name)
221 throw new Exception ();
223 // read to next element
224 while (reader.Read () && reader.NodeType != XmlNodeType.Element);
228 // Find right table from tablecollection
229 for (int i = 0; i < DataSet.Tables.Count && dt == null; i++) {
230 if (reader.Name == DataSet.Tables [i].TableName) {
231 dt = DataSet.Tables [i];
232 dt.ColumnChanged += new DataColumnChangeEventHandler (OnDataTableColumnChanged);
237 // TODO: Findout what kind of exception
239 throw new Exception (); // there were no correct table
241 while ((reader.NodeType != XmlNodeType.EndElement ||
242 reader.Name != dt.TableName) && reader.Read()) {
244 switch (reader.NodeType) {
246 case XmlNodeType.Element:
247 dt.Rows.Add (LoadRow (reader, dt.NewRow ()));
254 } while (reader.Read ());
256 base.Load (textReader);
259 #endregion // overloaded Load methods
262 public override void WriteContentTo(XmlWriter xw) {
263 base.WriteContentTo (xw);
267 public override void WriteTo(XmlWriter w) {
271 #endregion // Public Methods
273 #region Protected Methods
275 //FIXME: how do you handle this?
277 //protected internal override XPathNavigator CreateNavigator(XmlNode node) {
278 // throw new NotImplementedException();
282 public new XPathNavigator CreateNavigator() {
283 throw new NotImplementedException();
286 #endregion // Protected Methods
288 #region Private Methods
291 private void OnDataTableColumnChanged(object sender,
292 DataColumnChangeEventArgs eventArgs)
294 // row is not yet in datatable
295 if (eventArgs.Row.XmlRowID == 0)
298 // TODO: Here should be some kind of error checking.
299 GetElementsByTagName (eventArgs.Column.ToString ()) [dataRowIDList.IndexOf (
300 eventArgs.Row.XmlRowID)].InnerText = (string)eventArgs.ProposedValue;
306 private static void OnDataTableRowDeleted(object sender,
307 DataRowChangeEventArgs eventArgs)
309 throw new NotImplementedException();
313 private DataRow LoadRow (XmlReader reader, DataRow row)
315 // This method returns DataRow filled by values
317 string rowname = reader.Name;
320 if (reader.NodeType == XmlNodeType.Element)
321 column = reader.Name;
325 if (reader.NodeType == XmlNodeType.Text) {
327 string val = reader.Value;
328 if (row.Table.Columns.Contains (column))
332 // Every row must have unique id.
333 row.XmlRowID = dataRowID;
334 dataRowIDList.Add (dataRowID);