Linq over xml question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ilyas

    Linq over xml question

    Hi all

    I have a List of person objects using Generics. Each person will have
    an Id, firstname and may have a DepartmentId

    I am trying to convert this list to xml. However if the person doesnt
    have a departmentId, I dont want to write out a departmentId
    attribute. However I dont know how I can achieve this... I have tried
    (assuming personsDataSour ce1 is my list):

    XElement x1 =
    new XElement("root" ,
    new XElement("perso ns",
    from p in personsDataSour ce1
    select
    new XElement("perso n",
    new XAttribute("fir stname", p.Firstname),
    new XAttribute("Id" , p.Age)
    new XAttribute("dep arementId", p.DepartmentId)
    )));

    I need some conditional check againt the deparementId attribute, and
    only write it if that conditon is true - How can I do this using Linq
    to XML?

    Many thanks
  • Alex Meleta

    #2
    Re: Linq over xml question

    Hi Ilyas,

    Then use 'where' conditions for it:
    .... from p in personsDataSour ce1
    where p.DepartmentId == null
    select new XElement("perso n", .... or?

    ps. the example above is for case when the 'doesn't have a departmentId'
    means having null.
    Regards, Alex
    [TechBlog] http://devkids.blogspot.com

    IHi all
    I>
    II have a List of person objects using Generics. Each person will have
    Ian Id, firstname and may have a DepartmentId
    I>
    II am trying to convert this list to xml. However if the person doesnt
    Ihave a departmentId, I dont want to write out a departmentId
    Iattribute. However I dont know how I can achieve this... I have tried
    I(assuming personsDataSour ce1 is my list):
    I>
    IXElement x1 =
    Inew XElement("root" ,
    Inew XElement("perso ns",
    Ifrom p in personsDataSour ce1
    Iselect
    Inew XElement("perso n",
    Inew XAttribute("fir stname", p.Firstname),
    Inew XAttribute("Id" , p.Age)
    Inew XAttribute("dep arementId",
    Ip.DepartmentId )
    I)));
    II need some conditional check againt the deparementId attribute, and
    Ionly write it if that conditon is true - How can I do this using Linq
    Ito XML?
    I>
    IMany thanks
    I>



    Comment

    • Martin Honnen

      #3
      Re: Linq over xml question

      Ilyas wrote:
      I need some conditional check againt the deparementId attribute, and
      only write it if that conditon is true - How can I do this using Linq
      to XML?
      Can you show us the type of p.DepartmentId and tell us exactly which
      value (null?) the property has when you say a person does not have a
      department id?

      If the department id is a reference type and is null when you say a
      person does not have an id then you can simply check
      person.Id != null ? new XAttribute("id" , person.Id) : null
      e.g. in this example

      List<Personpers ons = new List<Person>() { new Person() {
      Name = "Foo", Id = "P1" }, new Person() { Name = "Bar" } };
      XDocument personDoc =
      new XDocument(
      new XElement("perso ns",
      from p in persons
      select new XElement("perso n",
      new XAttribute("nam e", p.Name),
      p.Id != null ? new XAttribute("id" , p.Id) :
      null)));
      personDoc.Save( Console.Out);

      the resulting XML is

      <persons>
      <person name="Foo" id="P1" />
      <person name="Bar" />
      </persons>

      The class Person looks like this:

      class Person
      {
      public string Name { get; set; }
      public string Id { get; set; }
      }

      so Id is of type string and a reference type.

      --

      Martin Honnen --- MVP XML

      Comment

      Working...