Solved

Working with XML

Posted on 2011-03-18
9
445 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:knowlton
  • 6
  • 3
9 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 35171425
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
 
LVL 5

Author Comment

by:knowlton
ID: 35181827
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
 
LVL 5

Author Comment

by:knowlton
ID: 35182009
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 35185598
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
 
LVL 5

Author Comment

by:knowlton
ID: 35185686
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
 
LVL 5

Author Closing Comment

by:knowlton
ID: 35185688
As usual, a terrific job!  :)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35185708
If you have another unanswered question, you can give me the URL...
0
 
LVL 5

Author Comment

by:knowlton
ID: 35185713
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
 
LVL 5

Author Comment

by:knowlton
ID: 35185834
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question