writing xml in C#.net

xml India
  • 13 years ago
    hello,

    I am new to both the xml and C#.net technologies. I am generating a xml file in c#.net.I am trying to generate a xml file whose first/root node should look like this:


    <tns:schedule xmlns:tns="http://www.sample.com/Schedule" xmlns:dt="http://www.sample.com/DataTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sample.com/Schedule Schedule.xsd" scheduleType="Playback" version="1.0" creationTime="2006-01-29T00:00:00" originator="sample">
      <scope startTime="2006-01-29T04:00:00" stopTime="2006-01-29T06:00:00" />

    I am using the xmlWriter class to generate the xml. The data for xml is taken from the structure which is filled by the program itself.

    <u>My code is as follows:</u>

    private void generateXml()
            {
                TreeNode tempNode,selectedNode;
                frmFindByProgram.stProgram stTemp = new frmFindByProgram.stProgram();
                selectedNode=new TreeNode();
                tempNode = new TreeNode();
                XmlTextWriter writer = new XmlTextWriter("E:\\Projects\\Project1\\July\\26\\ScheduleDemo\\NewXML.xml", new System.Text.UTF8Encoding());
            
                
               //Write the header information            
                writer.WriteStartDocument();  

                //Write the root node            
                 writer.WriteStartElement("tns", "schedule","http://www.sample.com/Schedule");  
            
                //use the record structure
                int i = 0, count;
                count = xmlSlotNodes.Count;    
                // sort the slots according to there callSign
                xmlSlotNodes.Sort(0,count,new Sorting ());
                string[] prgmTime = new string[2];
                
                stTemp = (frmFindByProgram.stProgram)xmlSlotNodes[i];
                selectedNode=getClickedNode();
                writer.Formatting = Formatting.Indented;

                writer.WriteStartElement("CruiseLine");
               // writer.WriteAttributeString("Id", selectedNode.Text );
                writer.WriteAttributeString ("Name",selectedNode.Parent.Text);

                writer.WriteStartElement("Ship");
                writer.WriteAttributeString("Name", selectedNode.Text  );
                
                writer.WriteStartElement("Channel");
                writer.WriteStartElement("CallSign");
                writer.WriteAttributeString("Value", stTemp.CallSign);
                prgmTime = stTemp.Duration.Split('-');

                do
                {
                    stTemp = (frmFindByProgram.stProgram)xmlSlotNodes[i];
                    prgmTime = stTemp.Duration.Split('-');                
                                
                    writer.WriteStartElement("Program");
                    writer.WriteAttributeString("Id", stTemp.ProgramID);
                    writer.WriteAttributeString("Title", stTemp.Title);
                    writer.WriteAttributeString("AssetName", stTemp.AssetName);
                    writer.WriteAttributeString("StartTime", prgmTime[0].ToString());
                    writer.WriteAttributeString("EndTime", prgmTime[1].ToString());    
                  
                    writer.WriteEndElement();
                    //Write some white space between nodes
                    writer.WriteWhitespace("\n");

                    tempNode =selectedNode;
                    selectedNode = getClickedNode();

                    -----
                    -----
                    ---
                    writer.close();
                }
    <u>my output file, NewXML.xml file contains the following </u>:

    <?xml version="1.0" encoding="utf-8" ?>
    - <tns:schedule xmlns:tns="http://www.sample.com/Schedule" >
    - <CruiseLine Name="Carnival">
    - <Ship Name="Celebration">
    - <Channel>
    - <CallSign Value="ABC">
      <Program Id="104" Title="assde4" AssetName="asset4" StartTime="01:30" EndTime="02:00" />
      </CallSign>
    ......
    </tns:schedule>

    <u>In order to modify the root/first node's information as given in the my sample file....</u>

    i added new line to the code that is at:
    //Write the root node            
    writer.WriteStartElement("tns", "schedule","http://www.sample.com/Schedule");  
    writer.WriteAttributeString("dt",null,"http://www.sample.com/DataTypes");



    - <tns:schedule d1p1:dt="" xmlns:d1p1="http://www.sample.com/DataTypes" xmlns:tns="http://www.sample.com/Schedule">


    i don't know from where this d1p1 got inserted and also why this attribute instead of getting appended is becoming the first attribute of the root element.

    Please can anyone help me out.I am trying it from last 4 days..  please try to help me soon.

    Thank you.
    Anee

















































































































Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold