Working with XML

I am trying to use XML to represent a

menu
- submenu
-- subsubmenu


structure.


Here is the entire XML:

<CATALOGS><CATALOG><CATID></CATID><DESC>CLOTHING</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS><CATALOG><CATID></CATID><DESC>MEN'S CLOTHING</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS><CATALOG><CATID>132</CATID><DESC>PANTS</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG><CATALOG><CATID>134</CATID><DESC>SHORTS</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG><CATALOG><CATID>136</CATID><DESC>SHIRTS</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG></NAVBARSUBCATS></CATALOG><CATALOG><CATID>140</CATID><DESC>WOMEN'S CLOTHING</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS><CATALOG><CATID>142</CATID><DESC>PANTS</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG><CATALOG><CATID>144</CATID><DESC>SHORTS</DESC><CONTENTPATH>somefolder/somepage.aspx</CON
TENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG><CATALOG><CATID>146</CATID><DESC>SHIRTS</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG><CATALOG><CATID>148</CATID><DESC>ACCESSORIES</DESC><CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG></NAVBARSUBCATS></CATALOG><CATALOG><CATID>140</CATID><DESC>CHILDREN'S CLOTHING</DESC><CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH><NAVBARSUBCATS><CATALOG><CATID>152</CATID><DESC>PANTS</DESC><CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG><CATALOG><CATID></CATID><DESC>SHORTS</DESC><CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG><CATALOG><CATID>156</CATID><DESC>SHIRTS</DESC><CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG><CATALOG><CATID>158</CATID><DESC>TOYS</DESC><CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS></CATALOG></
NAVBARSUBCATS></CATALOG></NAVBARSUBCATS></CATALOG></CATALOGS>

Open in new window




1)  Is it malformed?


2)  I do get a runtime error when I try to fill the DataSet object by reading in the XML:

{System.Data.DuplicateNameException: A column named 'NAVBARSUBCATS' already belongs to this DataTable: cannot set a nested table name to the same name.



{System.Data.DuplicateNameException: A column named 'NAVBARSUBCATS' already belongs to this DataTable: cannot set a nested table name to the same name.
   at System.Data.DataColumnCollection.RegisterColumnName(String name, DataColumn column, DataTable table)
   at System.Data.DataRelation.set_Nested(Boolean value)
   at System.Data.XSDSchema.InstantiateTable(XmlSchemaElement node, XmlSchemaComplexType typeNode, Boolean isRef)
   at System.Data.XSDSchema.HandleTable(XmlSchemaElement node)
   at System.Data.XSDSchema.HandleParticle(XmlSchemaParticle pt, DataTable table, ArrayList tableChildren, Boolean isBase)
   at System.Data.XSDSchema.HandleComplexType(XmlSchemaComplexType ct, DataTable table, ArrayList tableChildren, Boolean isNillable)
   at System.Data.XSDSchema.InstantiateTable(XmlSchemaElement node, XmlSchemaComplexType typeNode, Boolean isRef)
   at System.Data.XSDSchema.HandleTable(XmlSchemaElement node)
   at System.Data.XSDSchema.HandleParticle(XmlSchemaParticle pt, DataTable table, ArrayList tableChildren, Boolean isBase)
   at System.Data.XSDSchema.HandleComplexType(XmlSchemaComplexType ct, DataTable table, ArrayList tableChildren, Boolean isNillable)
   at System.Data.XSDSchema.InstantiateTable(XmlSchemaElement node, XmlSchemaComplexType typeNode, Boolean isRef)
   at System.Data.XSDSchema.HandleTable(XmlSchemaElement node)
   at System.Data.XSDSchema.HandleDataSet(XmlSchemaElement node, Boolean isNewDataSet)
   at System.Data.XSDSchema.LoadSchema(XmlSchemaSet schemaSet, DataSet ds)
   at System.Data.DataSet.InferSchema(XmlDocument xdoc, String[] excludedNamespaces, XmlReadMode mode)
   at System.Data.DataSet.ReadXml(XmlReader reader, Boolean denyResolving)
   at System.Data.DataSet.ReadXml(TextReader reader)
   at Campus_Webstore.NSubCatList.BuildCatalog() in C:\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\NSubCatList.ascx.cs:line 131}

Open in new window




3)  The use and reuse of <NAVBARSUBCATS> everywhere is on purpose.  Can I either get the DataSet to IGNORE or ALLOW the apparent duplicate table names....or is there a more suitable data structure to store the xml in?

Thanks!


Tom
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Bob LearnedConnect With a Mentor Commented:
Tom,

Here are my thoughts:

1) I prefer a formatted layout to visualize XML.  It was easy for me to reformat using Visual Studio.NET IDE:

<?xml version="1.0" encoding="utf-8" ?>
<CATALOGS>
  <CATALOG>
    <CATID></CATID>
    <DESC>CLOTHING</DESC>
    <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
    <NAVBARSUBCATS>
      <CATALOG>
        <CATID></CATID>
        <DESC>MEN'S CLOTHING</DESC>
        <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
        <NAVBARSUBCATS>
          <CATALOG>
            <CATID>132</CATID>
            <DESC>PANTS</DESC>
            <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
          <CATALOG>
            <CATID>134</CATID>
            <DESC>SHORTS</DESC>
            <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
          <CATALOG>
            <CATID>136</CATID>
            <DESC>SHIRTS</DESC>
            <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
        </NAVBARSUBCATS>
      </CATALOG>
      <CATALOG>
        <CATID>140</CATID>
        <DESC>WOMEN'S CLOTHING</DESC>
        <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
        <NAVBARSUBCATS>
          <CATALOG>
            <CATID>142</CATID>
            <DESC>PANTS</DESC>
            <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
          <CATALOG>
            <CATID>144</CATID>
            <DESC>SHORTS</DESC>
            <CONTENTPATH>
              somefolder/somepage.aspx</CONTENTPATH><NAVBARSUBCATS></NAVBARSUBCATS>
            </CATALOG>
          <CATALOG>
            <CATID>146</CATID>
            <DESC>SHIRTS</DESC>
            <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
          <CATALOG>
            <CATID>148</CATID>
            <DESC>ACCESSORIES</DESC>
            <CONTENTPATH>somefolder/somepage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
        </NAVBARSUBCATS>
      </CATALOG>
      <CATALOG>
        <CATID>140</CATID>
        <DESC>CHILDREN'S CLOTHING</DESC>
        <CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH>
        <NAVBARSUBCATS>
          <CATALOG>
            <CATID>152</CATID>
            <DESC>PANTS</DESC>
            <CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
          <CATALOG>
            <CATID></CATID>
            <DESC>SHORTS</DESC>
            <CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
          <CATALOG>
            <CATID>156</CATID>
            <DESC>SHIRTS</DESC>
            <CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
          <CATALOG>
            <CATID>158</CATID>
            <DESC>TOYS</DESC>
            <CONTENTPATH>somefolder/sompage.aspx</CONTENTPATH>
            <NAVBARSUBCATS></NAVBARSUBCATS>
          </CATALOG>
      </CATALOG>
    </NAVBARSUBCATS>
  </CATALOG>
</CATALOGS>

Open in new window


2) I see after reformatting that it doesn't have the proper end tag, and I get an error that confirms that:

      Expecting end tag </NAVBARSUBCATS>

3) What version of .NET do you have, because there are a lot better ways in 3.5/4 to work with XML, versus using a DataSet, such as LINQ-to-XML, DataContractSerializer, ...

4) XHTML suggests something that I agree with--lowercase

5) You have a format that is confusing, right from the git' go.  It is not a good practice to have the same element name for elements lower in the hierarchy.

<CATALOGS>
  <CATALOG catid="" desc="CLOTHING" contentPath="somefolder/somepage.aspx">
    <NAVBARSUBCATS>
      <CATALOG>

6) Use attributes, and self-closing tags to reduce the amount of text.

7) I could imagine something like this:

<?xml version="1.0" encoding="utf-8" ?>
<catalogs>
  <content>
    <catalog id="1" title="Summer Sales 2011" url="" />
    <catalog id="2" title="Fall Fling 2011" url="" />
    <catalog id="3" title="Sports and Leisure 2011" url="" />
  </content>
  <categories>
    <category id="1000" title="Seasonal" />
  </categories>
  <navigation>
    <menu menuId="1" type="category" elementId="1000" parentId="" />
    <menu menuId="2" type="catalog" id="1" parent="1" />
    <menu menuId="3" type="catalog" id="2" parent="1" />
    <menu menuId="4" type="catalog" id="3" parent="1" />
  </navigation>
</catalogs>

Open in new window

0
 
Tom KnowltonWeb developerAuthor Commented:
1)  Thanks for the reformat.

2)  Noted, and thank you!

3)  Visual Studio 2010.  But I don't get LINQ.  To me, LINQ is as difficult to understand as anything preceding it.

4)  I'll make the change!

5)  I didn't know this was not allowed.  I'm open to changing it as long as I can get the results.

6)  No problem here, as long as I can get the results I'm desiring!


I like #7 a lot.  Let me spend some time with it and see if it will do the trick.  As I said, I am attempting to use XML to represent a menu / submenu / sub submenu type structure.  Eventually this will be a left hand side navigation bar on a web page.
0
 
Tom KnowltonWeb developerAuthor Commented:
I am getting illegal characters in path, but the error does not say which characters:

- <catalogs>
- <content>
  <catalog id="1" title="Summer Sales 2011" url="" /> 
  <catalog id="2" title="Fall Fling 2011" url="" /> 
  <catalog id="3" title="Sports and Leisure 2011" url="" /> 
  </content>
- <categories>
  <category id="1000" title="Seasonal" /> 
  </categories>
- <navigation>
  <menu menuId="1" type="category" elementId="1000" parentId="" /> 
  <menu menuId="2" type="catalog" id="1" parent="1" /> 
  <menu menuId="3" type="catalog" id="2" parent="1" /> 
  <menu menuId="4" type="catalog" id="3" parent="1" /> 
  </navigation>
  </catalogs>

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Bob LearnedCommented:
Tom,

I am not sure what you are showing me, but the text has '-' preceding the node elements.  I imagine that this is a paste from Internet Explorer, but I need to check, since they are invalid characters.  Usually, when you get any errors with XML, it tells you what line and character position.  I would need to know that, in order to help you.
0
 
Tom KnowltonWeb developerAuthor Commented:
Since I posted...I have made it past that issue.

Here is my current XML, which seems to be working okay:


- <catalogs>
- <content>
  <catalog id="1" catid="" menutext="Womens" contenturl="" parentid="" /> 
  <catalog id="2" catid="" menutext="Mens" contenturl="" parentid="" /> 
  <catalog id="3" catid="" menutext="Childrens" contenturl="" parentid="" /> 
  <catalog id="4" catid="" menutext="Hats" contenturl="" parentid="" /> 
  <catalog id="5" catid="" menutext="Accessories" contenturl="" parentid="" /> 
  <catalog id="6" catid="" menutext="Gifts" contenturl="" parentid="" /> 
  <catalog id="7" catid="" menutext="Sweats" contenturl="" parentid="2" /> 
  <catalog id="8" catid="" menutext="Tees" contenturl="" parentid="2" /> 
  <catalog id="9" catid="" menutext="Shorts" contenturl="" parentid="2" /> 
  <catalog id="10" catid="" menutext="Football" contenturl="" parentid="2" /> 
  <catalog id="11" catid="" menutext="Basketball" contenturl="" parentid="2" /> 
  <catalog id="12" catid="" menutext="Accessores" contenturl="" parentid="2" /> 
  <catalog id="13" catid="" menutext="Post Season" contenturl="" parentid="2" /> 
  </content>
  </catalogs>


My new problem is with the jquery accordion now.

Open in new window


0
 
Tom KnowltonWeb developerAuthor Commented:
As usual, a terrific job!  :)
0
 
Bob LearnedCommented:
If you have another unanswered question, you can give me the URL...
0
 
Tom KnowltonWeb developerAuthor Commented:
Will do.


I am the lone developer at my small company with a deadline.

I could use the help!

Give me just a minute....
0
All Courses

From novice to tech pro — start learning today.