Solved

Produce xsd file

Posted on 2013-02-05
5
188 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String manipulation 15 64
Visual Studio 2013 debugging 2 35
Iterate two lists and remove inactive 11 49
how to read json value 2 29
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now