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

一个人想着一个人 提交于 2020-01-03 03:05:49

问题


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.Close();
      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


回答1:


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);
}



回答2:


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);
   }

}



回答3:


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();
}



回答4:


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.



来源:https://stackoverflow.com/questions/2667063/dataset-xml-document-load-dataset-into-an-xml-document-c-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!