Select to view content in your preferred language

How to write spatial query results into XML files?

2369
5
11-14-2010 09:11 AM
DanDong
Deactivated User
Hi all,

I work on my prj with VS2010 C#and ArcGIS silverlight API 2.0. Right now I have get results from spatial query and I want to write the results to XML files. And I have been given the xsd files about the format of these XML files.

I am a rookie here, so I am not sure whether my understanding is right or not.
First, I think I should use the xsd file to generate a class that corresponds to the specified schema in the C# language, and saves it as XXXXX.cs in the current directory.
But...I can't fine the xsd.exe in VS2010, does anybody know where it is? and how can put a command to generate a class from the XSD file?

Then if I want to write the results into XML file by using the schema provided by xsd file, what should I do?

I am really a new here. So I guess my questions are stupid. 😛 Are there any suggestions or samples? Thanks very much!
0 Kudos
5 Replies
DanDong
Deactivated User
Just asked my friend, at least the logic is right.

I have already used the xsd file to generate a cs file through command prompt. This cs file is the schema of XML file.
The command is "xsd /classes /language:CS XSDSchemaFile.xsd"

Are there any suggestions on how to use this cs file to write the results from a collection into XML file? Thank you 🙂
0 Kudos
JenniferNery
Esri Regular Contributor
This seems like a related thread, kindly see: http://forums.arcgis.com/threads/8774-save-layer-to-xml-file

While you will not be saving an entire layer to XML, the idea is the same, instead of layer.Graphics you will be using
ESRI.ArcGIS.Client.Tasks.QueryEventArgs args.FeatureSet.FeatureSet.
0 Kudos
DanDong
Deactivated User
This seems like a related thread, kindly see: http://forums.arcgis.com/threads/8774-save-layer-to-xml-file

While you will not be saving an entire layer to XML, the idea is the same, instead of layer.Graphics you will be using
ESRI.ArcGIS.Client.Tasks.QueryEventArgs args.FeatureSet.FeatureSet.


Thank you Jennifer! That's what I want 🙂

I write codes like this:

XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = "";
                settings.NewLineOnAttributes = true;

                //Using XmlWriter to create xml file
                //using (XmlWriter writer=XmlWriter .Create("C:\xmlfile.xml",XmlWriterSettings))
                string xmlfile = @"layer1_xml.xml";
                using (XmlWriter writer = XmlWriter.Create(xmlfile, settings ))
                {
                    writer.WriteComment ("Cutting results from layer1");
                    writer.WriteStartElement ("Results Details");
                    writer.WriteAttributeString  (featureSet.Features[0].Attributes.ElementAt(0).Key.ToString(),featureSet.Features[0].Attributes.ElementAt(0).Value.ToString());
                    writer.WriteEndElement();
                    //writer.Flush ;
                }


But it seems like there is a mistake in the writer.creater. The error is:
Error 1 The best overloaded method match for 'System.Xml.XmlWriter.Create(System.Xml.XmlWriter, System.Xml.XmlWriterSettings)' has some invalid arguments D:\AGS_mxd\dan\Final_backup\Final\MainPage.xaml.cs 907 43 Final

I think I write the parameters correctly, but still can't figure this out. Any suggestions?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Is your application WPF or Silverlight?
The XLMWriter class is depending on this.

With Silverlight, you can't create an XMLWriter writing to a file.

So your code should be OK with WPF but not with Silverlight:
 
               string xmlfile = @"layer1_xml.xml";
                using (XmlWriter writer = XmlWriter.Create(xmlfile, settings ))
 


With SL, you need a stream as argument.
Something like:
 
string dataFile = @"graphics.xml";
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = store.CreateFile(dataFile))
{
using (XmlWriter writer = XmlWriter.Create(fileStream, new XmlWriterSettings() { Indent = true }))
{
graphicsLayer.SerializeGraphics(writer);
writer.Close();
}
fileStream.Close();
}�??
0 Kudos
DanDong
Deactivated User
Is your application WPF or Silverlight?
The XLMWriter class is depending on this.

With Silverlight, you can't create an XMLWriter writing to a file.

So your code should be OK with WPF but not with Silverlight:
 
               string xmlfile = @"layer1_xml.xml";
                using (XmlWriter writer = XmlWriter.Create(xmlfile, settings ))
 



With SL, you need a stream as argument.
Something like:
 
string dataFile = @"graphics.xml";
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = store.CreateFile(dataFile))
{
using (XmlWriter writer = XmlWriter.Create(fileStream, new XmlWriterSettings() { Indent = true }))
{
graphicsLayer.SerializeGraphics(writer);
writer.Close();
}
fileStream.Close();
}�??


Thanks very much Dominique! My prj is silverlight, no wonder I can't create an XMLWriter writing to a file. I will try your codes. 🙂
0 Kudos