locked
Design question about generate hierachial XML RRS feed

  • Question

  • User1725185819 posted

    Objective is to generate hierarchial xml

     

    Input data is in the form of spreadsheet.

    Which has colums H1, H2, H3 H4, H5, startdate,  enddate, height, width, phases and rating

    I want to generate hirerichial xml

    H1  H2   H3    H4      H5

    That means H4 is parent of H5, H3 is parent of H4 and so on......

    A format of an xml will be

    <SetEquipment   H1Id=<variable H1 from excel>   type='H1'>   <Address height=<variable height>  width =<variable width>  />

    <SetEquipment  H2Id=<Variable H2 from excel>  type ='H2'>     <SetParentEquipment  ParentEquipmentId=<variable H1 from excel> type='H1'>        <ParentLinkRange Start=<Variable startdate from excel> End=<Variable enddate from excel> />     </SetParentEquipment>      <Address height=<variable height from excel> width=<variable width from excel> /> </SetEquipment>

    <SetEquipment  H3Id=<Variable H3 from excel>  type ='H2'>     <SetParentEquipment  ParentEquipmentId=<variable H2 from excel> type='H2'>        <ParentLinkRange Start=<Variable startdate from excel> End=<Variable enddate from excel> />     </SetParentEquipment>    </SetEquipment>

    <SetEquipment  H4Id=<Variable H4 from excel>  type ='H4'>     <SetParentEquipment  ParentEquipmentId=<variable H3 from excel> type='H3'>        <ParentLinkRange Start=<Variable startdate from excel> End=<Variable enddate from excel> />     </SetParentEquipment>     <TransformerAttributes RatingKVA=<Variable rating from excel> />     </SetEquipment>

    <SetEquipment  H5Id=<Variable H5 from excel>  type ='H5'>     <SetParentEquipment  ParentEquipmentId=<variable H4 from excel> type='H4'>        <ParentLinkRange Start=<Variable startdate from excel> End=<Variable enddate from excel> />     </SetParentEquipment>     <SPAttributes NumberOfPhases=<Variable phases from excel> />    </SetEquipment>

    In Visual studio 2012 I am planning to get a data from spreadsheet into a dataset

    Create the following classes SetEquipment SetParentEquipment Address TransformerAttributes SPAttributes

    using the namespace system.linq to generate xml

    Question:

    If anyone has any suggestion for better design this can be done I am very much interested

    Thanks

     

    Wednesday, February 18, 2015 10:13 PM

Answers

  • User-271186128 posted

    Hi sairam,

    From your description, it seems that you want to serialize objects into XML documents. If that is the case, I suggest you could refer to the following code:

            private void CreatePO2(string filename)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(H1));
                TextWriter writer = new StreamWriter(filename);
    
                H1 h1 = new H1();
                H5 h5 = new H5();
                h5.H5_Name = "H5_EEE";
    
                H4 h4 = new H4();
                h4.ClassH5 = h5;
                h4.H4_Name = "H4_DDD";
                H3 h3 = new H3();
                h3.H3_Name = "H3_CCCC";
                h3.ClassH4 = h4;
                H2 h2 = new H2();
                h2.H2_Name = "H2_BBB";
                h2.ClassH3 = h3;
    
                h1.H1_Name = "H1_AAA";
                h1.ClassH2 = h2;
    
                serializer.Serialize(writer, h1);
                writer.Close();
            }
        }
    
        [XmlRootAttribute("H1", Namespace = "http://www.cpandl.com",
        IsNullable = false)]
        public class H1
        {
            public string H1_Name;
            [XmlElement("H2")]
            public H2 ClassH2;
        }
        public class H2
        {
            public string H2_Name;
            [XmlElement("H3")]
            public H3 ClassH3;
        }
        public class H3
        {
            public string H3_Name;
            [XmlElement("H4")]
            public H4 ClassH4;
        }
        public class H4
        {
            public string H4_Name;
            [XmlElement("H5")]
            public H5 ClassH5;
        }
        public class H5
        {
            public string H5_Name { get; set; }
        }

    The output:

    <?xml version="1.0" encoding="utf-8"?>
    <H1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cpandl.com">
      <H1_Name>H1_AAA</H1_Name>
      <H2>
        <H2_Name>H2_BBB</H2_Name>
        <H3>
          <H3_Name>H3_CCCC</H3_Name>
          <H4>
            <H4_Name>H4_DDD</H4_Name>
            <H5>
              <H5_Name>H5_EEE</H5_Name>
            </H5>
          </H4>
        </H3>
      </H2>
    </H1>

    For more details about XmlSerializer, please see: https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-8

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 10, 2015 1:51 AM

All replies

  • User-2110585397 posted

    Hi Sairam,

    Form your description, my suggestion as below:

    <?xml version="1.0" encoding="utf-8" ?>
    <Root>
      <SetEquipment Id='1' type='H1' parentId='0' >
        <!--<ParentLinkRange start='' end=''></ParentLinkRange> Can be null.-->
        <Address height='' width =''></Address>
      </SetEquipment>
      <SetEquipment Id='2' type='H2' parentId='1' >
        <Address height='' width =''></Address>
        <ParentLinkRange start='' end=''></ParentLinkRange>
      </SetEquipment>
      <SetEquipment Id='3' type='H3' parentId='2' >
        <Address height='' width =''></Address>
        <ParentLinkRange start='' end=''></ParentLinkRange>
      </SetEquipment>
      <SetEquipment Id='4' type='H4' parentId='3' >
        <Address height='' width =''></Address>
        <ParentLinkRange start='' end=''></ParentLinkRange>
      </SetEquipment>
      <SetEquipment Id='5' type='H5' parentId='4' >
        <Address height='' width =''></Address>
        <ParentLinkRange start='' end=''></ParentLinkRange>
      </SetEquipment>
    </Root>
    

    Hope this can be helpful to you.

    Sherwin Zhao
    Best Regards

    Friday, February 20, 2015 7:23 AM
  • User1725185819 posted

    Hi Sherwin,

    It seems that you had misunderstood my question. Format of xml I had already listed in the post. I am looking for design approach through this I should handle.

    Moreover

     

    All the time all hierachy will not be present. For example H3 is not present. Then in this case dynamically H2 should become H4's paraent.

     

    Thanks

     

    Sunday, March 8, 2015 8:32 PM
  • User1725185819 posted

    My main objective is to generate hierachial xml

    In the dataset I have all the elements of hierarchy in one row

    For example H1, H2, H3, H4 ,H5............

    H1 is parent of H2

      H2 is child of H1 and parent of H3

        H3 is child of H2 and parent of H4

     H4 is child of H3 and parent of H5

        H5

    I am planning to define a generic class which will have  properties of all hierarchies (H1, H2...H5...).  If one property exists in more than one hierarchy, then it will exist only in a class

        For example

    Id Type RatingKVA Lat Long

    In the type I will  store H1, H2, H3.....

    In order to define hierachial relationship (for example, H1's child is H2..........)

    I am planning to call this generic class as list within itself.

    Is this good approch? Do you see any problem with it?

    Monday, March 9, 2015 9:27 AM
  • User-271186128 posted

    Hi sairam,

    From your description, it seems that you want to serialize objects into XML documents. If that is the case, I suggest you could refer to the following code:

            private void CreatePO2(string filename)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(H1));
                TextWriter writer = new StreamWriter(filename);
    
                H1 h1 = new H1();
                H5 h5 = new H5();
                h5.H5_Name = "H5_EEE";
    
                H4 h4 = new H4();
                h4.ClassH5 = h5;
                h4.H4_Name = "H4_DDD";
                H3 h3 = new H3();
                h3.H3_Name = "H3_CCCC";
                h3.ClassH4 = h4;
                H2 h2 = new H2();
                h2.H2_Name = "H2_BBB";
                h2.ClassH3 = h3;
    
                h1.H1_Name = "H1_AAA";
                h1.ClassH2 = h2;
    
                serializer.Serialize(writer, h1);
                writer.Close();
            }
        }
    
        [XmlRootAttribute("H1", Namespace = "http://www.cpandl.com",
        IsNullable = false)]
        public class H1
        {
            public string H1_Name;
            [XmlElement("H2")]
            public H2 ClassH2;
        }
        public class H2
        {
            public string H2_Name;
            [XmlElement("H3")]
            public H3 ClassH3;
        }
        public class H3
        {
            public string H3_Name;
            [XmlElement("H4")]
            public H4 ClassH4;
        }
        public class H4
        {
            public string H4_Name;
            [XmlElement("H5")]
            public H5 ClassH5;
        }
        public class H5
        {
            public string H5_Name { get; set; }
        }

    The output:

    <?xml version="1.0" encoding="utf-8"?>
    <H1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cpandl.com">
      <H1_Name>H1_AAA</H1_Name>
      <H2>
        <H2_Name>H2_BBB</H2_Name>
        <H3>
          <H3_Name>H3_CCCC</H3_Name>
          <H4>
            <H4_Name>H4_DDD</H4_Name>
            <H5>
              <H5_Name>H5_EEE</H5_Name>
            </H5>
          </H4>
        </H3>
      </H2>
    </H1>

    For more details about XmlSerializer, please see: https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-8

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 10, 2015 1:51 AM