3

I'm parsing an xml file.

the first line of xml looks like;

<DataSetMenu xmlns="http://tempuri.org/DataSetMenu.xsd">

I'm using below code to parse the xml.

string filename = Request.QueryString["file"].ToString();
XElement xdocument = XElement.Load(Server.MapPath("xml\\" + filename));

xdocument.Attribute("xmlns").Remove();

IEnumerable<XElement> MenuGroups = xdocument.Elements();

sb.Append("<link href='StyleSheet.css' rel='stylesheet' type='text/css' />");
foreach (var xel in MenuGroups)
{

    if (xel.Elements("MenuCatName").Any())
    {
        Int32 MenuCatID = Convert.ToInt32(xel.Element("MenuCatID").Value);
        string MenuCatName = xel.Element("MenuCatName").Value;
        sb.Append("<div class='CategoryDiv'><h1 class='category'>" + MenuCatName + " </h1>");
        GetGroupItems(xdocument, MenuCatID);
    }
}

But when ever xml file's first node contains :

 xmlns="http://tempuri.org/DataSetMenu.xsd"

It doesn't work at all. when i try to access xml's data it gives me error, Object reference not set to an instance of an object

XML FILE

    <DataSetMenu xmlns="http://tempuri.org/DataSetMenu.xsd">
    <MenuCategories>
     <MenuCatID>10108</MenuCatID>
     <MenuCatName>SPEICALS</MenuCatName>
     <BusinessEntityID>20137</BusinessEntityID>
    <DisplayIndex>10107</DisplayIndex>
     <MenuCatDesc />
      <Visible>true</Visible>
      <ImagePath />
   </MenuCategories>
   <MenuCategories>
    <MenuCatID>10109</MenuCatID>
    <MenuCatName>GENERAL MENU</MenuCatName>
    <BusinessEntityID>20137</BusinessEntityID>
     <DisplayIndex>10108</DisplayIndex>
      <   MenuCatDesc />
       <Visible>true</Visible>
       <ImagePath />
    </MenuCategories>
   <MenuGroups>
     <MenuGroupID>110079</MenuGroupID>
    <MenuCatID>10108</MenuCatID>
     <MenuGroupName>SPECIAL OFFERS</MenuGroupName>
     <MenuGroupDesc />
     <Visible>true</Visible>
     <DisplayIndex>0</DisplayIndex>
     <MenuTypeID>1</MenuTypeID>
    <ImagePath />
     <ServiceTimeEnforced>false</ServiceTimeEnforced>
    <ServiceStartTime>1900-01-01T11:00:00-06:00</ServiceStartTime>
     <ServiceEndTime>1900-01-01T15:00:00-06:00</ServiceEndTime>
     <Monday>true</Monday>
    <Tuesday>true</Tuesday>
    <Wednesday>true</Wednesday>
    <Thursday>true</Thursday>
    <Friday>true</Friday>
    <Saturday>true</Saturday>
    <Sunday>true</Sunday>
   </MenuGroups>
   </DataSetMenu>

Give me a solution so that it works fine. Or any way to remove this attribute.

Thanks.

Now It's now working here

 private void GetGroupItems(XElement xdocument, Int32 MenuCatID)
{

    var MenuGroup = from nm in xdocument.Elements("MenuGroups")
                    where (int)nm.Element("MenuCatID") == MenuCatID
                    select nm;

    foreach (XElement  GroupName in  MenuGroup)
    { 
        Int32 MenuGroupID = Convert.ToInt32(GroupName.Element("MenuGroupID").Value);
        string MenuGroupName = GroupName.Element("MenuGroupName").Value; 
        sb.Append("<div class='IType'>" + MenuGroupName + " </div>");
        sb.Append("<table class='restaurantlist'><tbody><tr><td class='righttd'>");
        GetMenuItems(MenuGroupID, xdocument);
        sb.Append("</td></tr></tbody></table>");
        sb.Append("<div class='restaurantlistdiv'><div style='clear: both;'></div>");
    }
}

please tell me, how should i modify this?

Code Rider
  • 2,003
  • 5
  • 32
  • 50

2 Answers2

2

You have to specify namespace when you're querying your XML.

Create XNamespace instance:

var ns = XNamespace.Get("http://tempuri.org/DataSetMenu.xsd");

Then use XNamespace + String operator implementation:

public static XName operator +(
    XNamespace ns,
    string localName
)

within Elements() method call, like that:

IEnumerable<XElement> MenuGroups = xdocument.Elements();

sb.Append("<link href='StyleSheet.css' rel='stylesheet' type='text/css' />");
foreach (var xel in MenuGroups)
{

    if (xel.Elements(ns + "MenuCatName").Any())
    {
        Int32 MenuCatID = Convert.ToInt32(xel.Element(ns + "MenuCatID").Value);
        string MenuCatName = xel.Element(ns + "MenuCatName").Value;
        sb.Append("<div class='CategoryDiv'><h1 class='category'>" + MenuCatName + " </h1>");
        GetGroupItems(xdocument, MenuCatID);
    }
}
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • ohh thanks a lot. Its working. But somewhere i'm getting a problem. Thats also because of namespace. Please tell me how to resolve it. I'm updating my question. thanks. – Code Rider Apr 02 '13 at 16:25
  • i hvae updated my question. Please check the last one code added. I need you little more help to resolve it. thanks. – Code Rider Apr 02 '13 at 16:27
  • right now i have given static namespace in "XNamespace.Get()". But if i want to make it dynamic. Because it may be possible that different xml files contain different namespaces. can i achieve this? – Code Rider Apr 03 '13 at 05:00
0

The key thing to remember here is that you are not working with an attribute, but the namespace declaration. That's why you can't just remove the attribute. See if the suggestions at C#: How to remove namespace information from XML elements help.

Community
  • 1
  • 1
Jim Wooley
  • 10,169
  • 1
  • 25
  • 43