none
Read Xml RRS feed

  • Question

  • hi, below is the sample code to play with

    <?xml version="1.0" encoding="UTF-8"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
        <xs:element name="UsersList">
        <xs:complexType>   
          <xs:sequence>   
            <xs:element ref="User" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
      </xs:element>
             
          <xs:element name="User">
            <xs:complexType>
              <xs:sequence>
                <xs:element ref="Name" />
                <xs:element ref="BirthTime" />
                </xs:sequence>
        </xs:complexType>
          </xs:element>
          
      
          <xs:element name = "Name">
           <xs:simpleType>
              <xs:restriction base = "xs:string">
                 <xs:pattern value="[a-zA-Z0-9]*"/>
              </xs:restriction>
           </xs:simpleType>
        </xs:element>
     
          <xs:element name="BirthTime">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:pattern value="(0[0-9]|[1][0-9]|2[0-3]):[0-5][0-9]"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:element>
           
        </xs:schema>

    xml:

    <?xml version="1.0" encoding="utf-8"?>
    <UsersList>
      <User>
        <Name>sam</Name>
        <BirthTime>20:11</BirthTime>
      </User>
      <User>
        <Name>wit</Name>
        <BirthTime>10:11</BirthTime>
      </User>
    </UsersList>

    class:

    namespace Test
    {
        using System;
        using System.Diagnostics;
        using System.Xml.Serialization;
        using System.Collections;
        using System.Xml.Schema;
        using System.ComponentModel;
        using System.Collections.Generic;
     
     
        public partial class UsersList
        {
     
            private List<User> userField;
     
            public UsersList()
            {
                this.userField = new List<User>();
            }
     
            public List<User> User
            {
                get
                {
                    return this.userField;
                }
                set
                {
                    this.userField = value;
                }
            }
        }
     
        public partial class User
        {
     
            private string nameField;
     
            private string birthTimeField;
     
            public string Name
            {
                get
                {
                    return this.nameField;
                }
                set
                {
                    this.nameField = value;
                }
            }
     
            public string BirthTime
            {
                get
                {
                    return this.birthTimeField;
                }
                set
                {
                    this.birthTimeField = value;
                }
            }
        }
    }

    tried to load the xml into list<user> as below

    XElement xelement = XElement.Load(@"D:\XmlTesting\user.xml");
     
              IEnumerable<XElement> lstuser = xelement.Elements();

    but result is nothing. any suggestion how to resolve this issue


    loving dotnet

    Monday, October 24, 2016 4:22 PM

All replies

  • lstuser should contain two XElements. You could realize the items by calling ToList() on the IEnumerable:

    List<XElement> lstuser = xelement.Elements().ToList();
    int count = lstuser.Count; // = 2

    If you want a List<User> you could use the LINQ Select method to create a new User object for each returned XElement:

    List<User> lstuser = xelement.Elements().Select(element => new User()
                {
                    Name = element.Element("Name").Value,
                    BirthTime = element.Element("BirthTime").Value,
                }).ToList();

    Or if you want a UserList:

                UsersList userList = new UsersList()
                {
                    User = xelement.Elements().Select(element => new User()
                    {
                        Name = element.Element("Name").Value,
                        BirthTime = element.Element("BirthTime").Value,
                    }).ToList()
                };

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, October 24, 2016 5:52 PM
  • Hi Magnus,

    thanks for your help and  i have question on the below code

    List<User> lstuser = xelement.Elements().Select(element => new User()
                {
                    Name = element.Element("Name").Value,
                    BirthTime = element.Element("BirthTime").Value,
                }).ToList();


    assume i have element called "Family" in thel same xml which is object and how do i call the object in the above method.

    My try :

    List<User> lstuser = xelement.Elements().Select(element => new User()
                {
                    Name = element.Element("Name").Value,
                    BirthTime = element.Element("BirthTime").Value,
    Family= element.Element("Family").Value,
                }).ToList();

    but it's not working. it says cannot convert from string to Family because Family is an object with below properties

    DadName,Momname.

    How do i achieve this. any suggestion please


    loving dotnet

    Monday, October 24, 2016 7:41 PM
  • Hi Born2Achieve,

    If you add element called "Family" in thel same xml which is object. it seems that you need to add related property on class named "User", like this:

     public partial class User
        {
            public string Name { get; set; }
            public string BirthTime { get; set; }
            public object Family { get; set; }
        }
    

    #XML

    <?xml version="1.0" encoding="utf-8"?>
    <UsersList>
      <User>
        <Name>sam</Name>
        <BirthTime>20:11</BirthTime>
        <Family>axxx</Family>
      </User>
      <User>
        <Name>wit</Name>
        <BirthTime>10:11</BirthTime>
        <Family>bbbx</Family>
      </User>
    </UsersList>

    #Usage:

    XElement xelement = XElement.Load("Test.xml");
    
                IEnumerable<XElement> lstuser = xelement.Elements();
    
                UsersList userlist = new UsersList();
    
                userlist.User = lstuser.Select(element => new User() {
                    Name = element.Element("Name").Value,
                    BirthTime = element.Element("BirthTime").Value,
                    Family = element.Element("Family").Value
                }).ToList();

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 25, 2016 2:22 AM
    Moderator
  • Please only ask one question per thread and then start a new thread if you have a new question.

    >>assume i have element called "Family" in thel same xml which is object and how do i call the object in the above method.

    You need to set the Family property to a Family object, e.g.:

    UsersList userList = new UsersList()
                {
                    User = xelement.Elements().Select(element => new User()
                    {
                        Name = element.Element("Name").Value,
                        BirthTime = element.Element("BirthTime").Value,
                        Family = new Family()
                        {
                            MomName = element.Element("Family").Element("MomName").Value,
                            DadName = element.Element("Family").Element("DadName").Value,
                        }
                    }).ToList()
                };

    The above sample code assumes that each User node in the XML data has a Family child node:

      <User>
        <Name>sam</Name>
        <BirthTime>20:11</BirthTime>
        <Family>
          <MomName>...</MomName>
          <DadName>...</DadName>
        </Family>
      </User>

    Please remember to close this thread by marking all helpful posts as answer.

    Tuesday, October 25, 2016 5:53 PM