locked
Need to create an XML from a class object RRS feed

  • Question

  • User-1404317421 posted

    Hi All,

    I'm joining 2 list objects and creating a master list data which is of type T: class and would like to serialize it to XML with the below format. How can i achieve this?

    <Reports>
      <Header>
        <PrintedDate>2016-07-01T15:16:09.473</PrintedDate>
        <PrintedBy>XYZ</PrintedBy>
      </Header>
      <Report>
        <Name>dd</Name>
        <UserName />
        <Remarks />
        <IPAddress>192.168.1.83</IPAddress>
        <CreatedDate>2015-10-07T17:48:35.243</CreatedDate>
        <AppID>OS</AppID>
        <AppVersion>0.0.0.2</AppVersion>
        <LoginDate>2015-10-07T17:48:05.380</LoginDate>
        <LogoutDate>1900-01-01T00:00:00</LogoutDate>
      </Report>
      <Report>
        <Name>ff</Name>
        <UserName />
        <Remarks />
        <IPAddress>192.168.1.83</IPAddress>
        <CreatedDate>2015-10-07T17:49:36.107</CreatedDate>
        <AppID>OS</AppID>
        <AppVersion>0.0.0.2</AppVersion>
        <LoginDate>2015-10-07T17:49:26.287</LoginDate>
        <LogoutDate>1900-01-01T00:00:00</LogoutDate>
      </Report>
    </Reports>

    This is my class file which i'm serializing

        [Serializable]
        [XmlRoot(ElementName = "Reports")]
        public class UserReportJoinedData
        {
            public DateTime CreatedDate { get; set; }
            public string LogType { get; set; }
            public string LoginID { get; set; }
            public string Name { get; set; }
            public string AppVersion { get; set; }
            public string System { get; set; }
            public string UserIPAddress { get; set; }
            public DateTime? LoginDate { get; set; }
            public DateTime? LogoutDate { get; set; }
            public string Remarks { get; set; }
    
            public UserReportJoinedData()
            {
    
            }
        }

    But, now I'm getting the XML in the below format,

    <ArrayOfUserReportJoinedData>
    <UserReportJoinedData>
    <CreatedDate>2015-10-07T17:48:35</CreatedDate>
    <LogType>Login</LogType>
    <LoginID>B1ADMIN</LoginID>
    <Name>ef</Name>
    <AppVersion/>
    <System>OS</System>
    <UserIPAddress>192.168.1.83</UserIPAddress>
    <LoginDate>2015-10-07T17:48:05</LoginDate>
    <LogoutDate xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" p3:nil="true"/>
    <Remarks/>
    </UserReportJoinedData>
    <UserReportJoinedData>
    <CreatedDate>2015-10-07T17:48:35</CreatedDate>
    <LogType>Login</LogType>
    <LoginID>B1ADMIN</LoginID>
    <Name>rr</Name>
    <AppVersion/>
    <System>OS</System>
    <UserIPAddress>192.168.1.83</UserIPAddress>
    <LoginDate>2015-10-07T17:48:05</LoginDate>
    <LogoutDate xmlns:p3="http://www.w3.org/2001/XMLSchema-instance" p3:nil="true"/>
    <Remarks/>
    </UserReportJoinedData>
    </ArrayOfUserReportJoinedData>

    I would like to replace UserReportJoinedData with Report and ArrayOfUserReportJoinedData to Reports and need to add <Header> section as shown in the required XML file above.

    Tuesday, July 19, 2016 6:50 AM

Answers

  • User-1404317421 posted

    I've resolved it of myself using XElement..

    Thanks !

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 19, 2016 9:29 AM