create customised XML c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nanban4u
    New Member
    • Nov 2006
    • 6

    create customised XML c#

    Hi,
    I have a select query like this
    Select value,key from table;
    value key
    a 101
    b 102
    a 103
    a 104

    I need to have xml like this

    <AvailableField s>
    <Table Name="table1">
    <Type Name="a">
    <Fields Key="101" />
    <Fields Key="103" />
    <Fields Key="104" />
    </Type>
    <Type Name="b">
    <Fields Key="103" />
    </Type>
    </Table>
    </AvailableFields >
  • Lok
    New Member
    • Nov 2006
    • 11

    #2
    Originally posted by nanban4u
    Hi,
    I have a select query like this
    Select value,key from table;
    value key
    a 101
    b 102
    a 103
    a 104

    I need to have xml like this

    <AvailableField s>
    <Table Name="table1">
    <Type Name="a">
    <Fields Key="101" />
    <Fields Key="103" />
    <Fields Key="104" />
    </Type>
    <Type Name="b">
    <Fields Key="103" />
    </Type>
    </Table>
    </AvailableFields >
    Then? what exactly do you want?

    Comment

    • nanban4u
      New Member
      • Nov 2006
      • 6

      #3
      I need to create a xml in this particular format...
      how to generate this customised xml using c# code
      <AvailableField s>
      <Type Name="a">
      <Fields Key="101" />
      <Fields Key="103" />
      <Fields Key="104" />
      </Type>
      <Type Name="b">
      <Fields Key="103" />
      </Type>
      </AvailableFields >

      Comment

      • Lok
        New Member
        • Nov 2006
        • 11

        #4
        It's depens on what your main program doing and how you store/manage your data inside C#.

        If you said you only want to generate the XML file and basicly XML file is just like an ordinary plain text file(that can be created using simple text editor like notepad), so it's just a matter of writing an in-memory string into external text file.

        by using System.IO.Strea mWriter you can open a file stream by calling it's constructor (eg. xmlstream = new StreamWriter("m yFileName.xml") ; ), then invoke it's xmlstream.Write Line("<myXmlTag myXmlAttribute = 'myAttrValue'>" ); for each line of your data.

        Regards,

        Lok

        Comment

        • Lok
          New Member
          • Nov 2006
          • 11

          #5
          now the next step.

          I assume you grab the data from the IDataReader. And I think you need to order your query based on the "value" using statement such as SELECT value,key FROM table1 ORDER BY value.

          After that, you just simply loop and using a variable to keep the last "value", if the "value" remain the same, so just print the <Fields key="...">. But if the content of "value" change, print </Type> as the closing element/tag for that old "value" , then print <Type Name="..."> as a new open element/tag for the new "value".

          The trick is here, put "" (empty string) as an initial value of the variable, then check if is it empty string, then no need to print the closing tag, but directly print the new open tag.

          regards,

          Lok

          Comment

          • nanban4u
            New Member
            • Nov 2006
            • 6

            #6
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<A vailableFields> <Table Name=\"table1\" ></Table></AvailableFields >");
            XmlNode tableNode = doc.DocumentEle ment.ChildNodes[0];

            DataSet data = new DataSet();
            DataTable table = new DataTable("tabl e1");
            data.Tables.Add (table);
            table.Columns.A dd(new DataColumn("val ue", typeof(string)) );
            table.Columns.A dd(new DataColumn("key ", typeof(int)));
            table.Columns.A dd(new DataColumn("id" , typeof(int)));

            DataRow newRow = table.NewRow();
            newRow.ItemArra y = new object[]{"a", 101, 1};
            table.Rows.Add( newRow);

            newRow = table.NewRow();
            newRow.ItemArra y = new object[]{"b", 102, 1};
            table.Rows.Add( newRow);

            newRow = table.NewRow();
            newRow.ItemArra y = new object[]{"a", 103, 2};
            table.Rows.Add( newRow);

            newRow = table.NewRow();
            newRow.ItemArra y = new object[]{"a", 104, 2};
            table.Rows.Add( newRow);

            foreach(DataRow row in data.Tables[0].Rows){
            string type = row["value"].ToString();
            string key = row["key"].ToString();
            string id = row["id"].ToString();

            XmlNode typeNode = tableNode.Selec tSingleNode("Ty pe[@Name = '" + type + "']");
            if (typeNode == null){
            typeNode = doc.CreateEleme nt("Type");
            ((XmlElement)ty peNode).SetAttr ibute("Name", type);
            tableNode.Appen dChild(typeNode );

            }
            XmlElement newField = doc.CreateEleme nt("Fields");
            newField.SetAtt ribute("Key", key);
            newField.SetAtt ribute("id", id);
            typeNode.Append Child(newField) ;
            }

            Console.WriteLi ne(doc.OuterXml );
            Console.ReadLin e();

            Comment

            Working...