Solved

Working with XML

Posted on 2011-03-18
9
433 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 5

Author Comment

by:knowlton
Comment Utility
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
Comment Utility
As usual, a terrific job!  :)
0
 
LVL 96

Expert Comment

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

Author Comment

by:knowlton
Comment Utility
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
Comment Utility
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now