2

I have xml files with following to generate the menu for our web site.

 <xs:element name="Menu">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="MenuItem" type="MenuItemType" maxOccurs="unbounded"></xs:element>
        </xs:sequence>
        <xs:attribute name="Title" type="xs:string"></xs:attribute>
        <xs:attribute name="Type" type="xs:string"></xs:attribute>
    </xs:complexType>

</xs:element>
<xs:complexType name="MenuItemType">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="MenuItem" type="MenuItemType" />
    </xs:choice>
    <xs:attribute name="Text" type="xs:string"></xs:attribute>
    <xs:attribute name="Url" type="xs:string"></xs:attribute>
</xs:complexType>

Right now I am using xmlserializer to convert these xml files in to Menu objects and use them to generate the menu. I want to use LINQ to xml to convert these xml files in to same object. Any help will be appreciated. Generated class for above xml file is

 public partial class Menu {

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("MenuItem")]
    public MenuItemType[] MenuItem;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Title;
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Type;
}
public partial class MenuItemType {

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("MenuItem")]
    public MenuItemType[] Items;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Text;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Url;
}
5
  • This would be easier if we could see the actual XML for a menu as opposed to the schema definition for same. Commented Nov 14, 2009 at 12:20
  • 1
    why would you want to use Linq-to-XML instead of deserializing them directly? Deserializing an XML into an object seems like the much easier and preferable way to do this.... Commented Nov 14, 2009 at 13:11
  • that is what I am doing right now. I am just interested to know how to convert an recursive xml in to object collection using LINQ. Commented Nov 14, 2009 at 14:43
  • @Murph, I have given schema because it represent the complete structure. Sample xml may not cover complete schema if schema contains any optional elements or attributes and we will not know how many child elements an elements contains. Commented Nov 14, 2009 at 14:48
  • 1
    @raj - fair point however you're still asking us to work with one hand tied behind our back, you've got examples and some of us at least would have to create them from scratch to work toward the desired result. Commented Nov 14, 2009 at 15:35

1 Answer 1

5

I haven't tested it. But, hope this works.

var o = (from e in XDocument.Load("").Elements("MenuItem")
         select new Menu
         {
             MenuItem = GenerateMenuItemType(e).ToArray(),
             Title = (string)e.Attribute("Title"),
             Type = (string)e.Attribute("Type")
         });

private IEnumerable<MenuItemType> GenerateMenuItemType(XElement element)
{
    return (from e in element.Elements("MenuItem")
            select new MenuItemType
            {
                Items = GenerateMenuItemType(e).ToArray(),
                Text = (string)e.Attribute("Title"),
                Url = (string)e.Attribute("Url")
            });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Mehdi, thanks a lot. It would be great if we can find a solution that can do in single query.
The problem is your Xml document structure.the first-children have a different structure with their child. if you can change the Xml document structure and make a new permanent schema for all of the menu items, it's so easy to write it in a query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.