?
Solved

Produce xsd file

Posted on 2013-02-05
5
Medium Priority
?
197 Views
Last Modified: 2013-02-19
Thanks to the answer I received here I was able to produce the xml file.
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_28015259.html

How do I control whether the top line says
<?xml version="1.0"?>  or <?xml version='1.0'?>  single or double quotes

How do I produce an xsd for the xml files I produce?

I'm writing a c# executable in visual studio 2008 .net framework 3.5.
0
Comment
Question by:AlHal2
  • 3
5 Comments
 
LVL 7

Accepted Solution

by:
tipsybroom earned 750 total points
ID: 38854381
0
 

Author Comment

by:AlHal2
ID: 38854445
I tried adding it just after I call the parsexml routine and get the message doc does not exist in the current context.

Not sure if it's relevant, but the xsd needs to be in a separate file.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.Xml.XPath ;
namespace ILXSymbolMasterExtract
{
    public partial class Form1 : Form
    {
       
        string[] arrAttrBond = {
                                "sedol","curr","localId","issuShortDesc","exch","longIssuerName", "symbol",
                                "action","sectyType","cusip","issuLongDesc","localCode","dfltInd",
                                "mat","isin","sess","issuerName"
                           };
        string[] arrAttrInd = {
                                "sedol","curr","localId","issuShortDesc","exch","longIssuerName", "symbol",
                                "action","sectyType","cusip","issuLongDesc","localCode","dfltInd",
                                "isin","sess","issuerName","sicCode"
                           };

        string[] arrAttrTr = {
                                "sedol","issuerName","mat","symbol","sess","issuShortDesc","cusip",
                                "issuLongDesc","longIssuerName","sectyType","action","isin","exch","curr",
                           };

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {


                ParseXml(@"C:\\CAMCont\Input\\symbol_master.xml", @"C:\\CAMCont\Output\\symbol_master_bond.xml", "bond", "bnd", arrAttrBond);
                ParseXml(@"C:\\CAMCont\Input\\symbol_master.xml", @"C:\\CAMCont\Output\\symbol_master_ei.xml", "index", "ind", arrAttrInd);
                ParseXml(@"C:\\CAMCont\Input\\symbol_master.xml", @"C:\\CAMCont\Output\\symbol_master_treasury.xml", "treasury", "tr", arrAttrTr);

                //XmlDeclaration xmldecl;
                //xmldecl = doc.CreateXmlDeclaration("1.0", null, null);

                ////Add the new node to the document.
                //XmlElement root = doc.DocumentElement;
                //doc.InsertBefore(xmldecl, root);
               


                //Console.WriteLine("Display the modified XML...");
                //doc.Save(Console.Out);

           
            }
               
            catch (Exception ex)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(ex.Message);
            }
            Environment.Exit(0);
           
        }
       

        private void ParseXml(string fn, string fnout,string InstTypeMain,string InstTypeMinor,string[] AttributeInst)
        {

            using (Stream stream = File.OpenRead(fn))
            {
               
                XmlTextReader r = new XmlTextReader(stream);
                r.WhitespaceHandling = WhitespaceHandling.None;
                r.MoveToContent();

                using (XmlTextWriter w = new XmlTextWriter(fnout,null ))
                {
                    w.Formatting = Formatting.Indented;
                    w.Indentation = 3;
                    //w.IndentChar = '\t';
                    w.IndentChar = ' ';
                    w.WriteStartDocument();
                    w.WriteStartElement("data");

                    int intParsingState = 0;

                    // changed the loop structure to start at root (not really necessary) but also to be able to use "continue" to skip r.Read() after r.Skip()...
                    do
                    {
                        switch (r.NodeType)
                        {
                            case XmlNodeType.Element:

                                if (r.LocalName == InstTypeMain)
                                {
                                    intParsingState = 1;
                                }
                                else if (r.LocalName == InstTypeMinor)
                                {
                                    if (intParsingState > 0)
                                    {
                                        w.WriteStartElement("n");
                                        foreach (string a in AttributeInst)
                                        {
                                            w.WriteElementString(a, r.GetAttribute(a).ToString());
                                        }
                                        w.WriteEndElement();
                                    }

                                }
                                else
                                {
                                    // skip children of other elements on the same depth as the <bond> element
                                    if (r.Depth >= 1)
                                    {
                                        r.Skip();
                                        continue; // don't call r.Read() in this case...
                                    }

                                }
                               
                                break;
                            case XmlNodeType.EndElement:
                               
                                if (r.LocalName == InstTypeMain)
                                {
                                    intParsingState = 2; // break do-while
                                }

                                break;
                            default:
                                break;
                        }
                        if (intParsingState < 2 && !r.Read())
                            break;
                    } while (intParsingState < 2);
                    w.WriteEndElement();
                    w.WriteEndDocument();
                    w.Flush();
                    w.Close();
                }
                r.Close();
            }
        }
       
    }
}
0
 
LVL 4

Expert Comment

by:Srinivasulu Muppala
ID: 38858828
Do you want to produce XSD for xml data?
0
 

Author Comment

by:AlHal2
ID: 38864442
yes please.
0
 

Author Closing Comment

by:AlHal2
ID: 38904989
A step in the right direction.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question