开发者

Dataset -> XML Document - Load DataSet into an XML Document - C#.Net

I'm trying to read a dataset as xml and load it into an XML Document.

XmlDocument contractHistoryXMLSchemaDoc = new XmlDocument();

using (MemoryStream ms = new MemoryStream())
{
   //XmlWriterSettings xmlWSettings = new XmlWriterSettings();

   //xmlWSettings.ConformanceLevel = ConformanceLevel.Auto;
   using (XmlWriter xmlW = XmlWriter.Create(ms))
   {
      xmlW.WriteStartDocument();
      dsContract.WriteXmlSchema(xmlW);
      xmlW.WriteEndDocument();
      xmlW.Cl开发者_如何学Pythonose();
      using (XmlReader xmlR = XmlReader.Create(ms))
      {
         contractHistoryXMLSchemaDoc.Load(xmlR);
      }
   }
}

But I'm getting the error - "Root Element Missing".

Any ideas?

Update

When i do xmlR.ReadInnerXML() it is empty. Does anyone know why?

NLV


A few things about the original code:

  • You don't need to call the write start and end document methods: DataSet.WriteXmlSchema produces a complete, well-formed xsd.
  • After writing the schema, the stream is positioned at its end, so there's nothing for the XmlReader to read when you call XmlDocument.Load.

So the main thing is that you need to reset the position of the MemoryStream using Seek. You can also simplify the whole method quite a bit: you don't need the XmlReader or writer. The following works for me:

XmlDocument xd = new XmlDocument();
using(MemoryStream ms = new MemoryStream())  
{
    dsContract.WriteXmlSchema(ms);

    // Reset the position to the start of the stream
    ms.Seek(0, SeekOrigin.Begin);
    xd.Load(ms);
}


XmlDocument contractHistoryXMLSchemaDoc = new XmlDocument();

using (MemoryStream ms = new MemoryStream())
{
   dsContract.WriteXml(ms);
   ms.Seek(0, SeekOrigin.Begin);
   using(XmlReader xmlR = XmlReader.Create(ms))
   {
       contractHistoryXMLSchemaDoc.Load(xmlR);
   }

}


All you really need to do is to call contractHistoryXMLSchemaDoc.Save(ms). That will put the xml into the MemoryStream.

XmlDocument contractHistoryXMLSchemaDoc = new XmlDocument();

using (MemoryStream ms = new MemoryStream())
{
    contractHistoryXMLSchemaDoc.Save(ms);
    ms.Flush();
}


Here you go. If from the naming convention of your variables, (not though the question you asked, which appears to change...), you need to load the XML scema of the data set into the XML document that you named with schema, below is the code to load schema of the dataset into an XMLDocument.

I want the schema of the dataset in a separate XML document. – NLV Apr 19 '10 at 12:01

Answer:

XmlDocument contractHistoryXMLSchemaDoc = new XmlDocument();
using (MemoryStream ms = new MemoryStream())
{
    dsContract.WriteXmlSchema(ms);
    ms.Seek(0, SeekOrigin.Begin);
    contractHistoryXMLSchemaDoc.Load(ms);
}

If you are looking for how to load the dataset table data into an XML document (note I removed the word Schema from the XMLDocument variable name)

Your Question:

Sorry, i dont get you. I need to get the xml of that dataset into an xml document. – NLV Apr 19 '10 at 11:56

Answer:

XmlDocument contractHistoryXMLDoc = new XmlDocument();
using (MemoryStream ms = new MemoryStream())
{
    dsContract.WriteXml(ms,XmlWriteMode.IgnoreSchema);
    ms.Seek(0, SeekOrigin.Begin);
    contractHistoryXMLDoc.Load(ms);
}

If you want the Schema and data set in separate documents, the code is above. If you want just the schema or just the data in and xml document, use the above bit of code that pertains to your question. If you want both the XML Schema and the Data in the same XMLDocument, then use this code.

XmlDocument contractHistoryXMLDoc = new XmlDocument();
using (MemoryStream ms = new MemoryStream())
{
    dsContract.WriteXml(ms,XmlWriteMode.WriteSchema);
    ms.Seek(0, SeekOrigin.Begin);
    contractHistoryXMLDoc.Load(ms);
}

Your Question:

But I'm getting the error - "Root Element Missing". Any ideas? Update When i do xmlR.ReadInnerXML() it is empty. Does anyone know why? NLV

Answer:

There are underlying issues in your code in the way you think it is working, which means it is not really creating the XMLSchema and XmlData (dsContract), which is why you are seeing a blank XMLDocument.

It would probably help you to fully explain what you wish, and then not reply to everyone using partial sentences of which are misunderstood and your having to keep replying with more partial sentences.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜