Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I group XML nodes of the same name?
    primarykey
    data
    text
    <p>I am trying to figure out how to group XML nodes with the same name but different values.</p> <p>The web service I'm using returns an XmlElement that looks like this:</p> <pre><code>&lt;Items&gt; &lt;Item&gt; &lt;Name name="Name"&gt;Item 1&lt;/Name&gt; &lt;Description name="Description"&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit. &lt;/Description&gt; &lt;AssociatedItems name="Associated Items"&gt;Item 2&lt;/AssociatedItems&gt; &lt;AssociatedItems name="Associated Items"&gt;Item 3&lt;/AssociatedItems&gt; &lt;AssociatedItems name="Associated Items"&gt;Item 4&lt;/AssociatedItems&gt; &lt;AssociatedItems name="Associated Items"&gt;Item 5&lt;/AssociatedItems&gt; &lt;/Item&gt; &lt;/Items&gt; </code></pre> <p>I am transforming each node into an HTML tag</p> <pre><code>protected void lnkItem_Click(object sender, EventArgs e) { LinkButton link = (LinkButton)sender; GridViewRow row = (GridViewRow)link.Parent.Parent; string id = gv.DataKeys[row.RowIndex]["id"].ToString(); PublicApiAsmxServiceSoapClient service = new PublicApiAsmxServiceSoapClient("PublicApiAsmxServiceSoap", WEB_SERVICE_URL); XmlElement xml = service.ItemGetAsXml(id); XElement nodes = XElement.Parse(xml.InnerXml); foreach (var node in nodes.Elements()) { InsertHTML(node); } } private void InsertHTML(XElement node) { if (node.Value == string.Empty) return; pnl.ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("&lt;h3&gt;{0}&lt;/h3&gt;", node.Attribute("name").Value))); pnl.ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("&lt;p&gt;{0}&lt;/p&gt;", node.Value))); } </code></pre> <p>With my code right now, the HTML output would be:</p> <pre><code>&lt;h3&gt;Name&lt;/h3&gt; &lt;p&gt;Item 1&lt;/p&gt; &lt;h3&gt;Description&lt;/h3&gt; &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/p&gt; &lt;h3&gt;Associated Items&lt;/h3&gt; &lt;p&gt;Item 2&lt;/p&gt; &lt;h3&gt;Associated Items&lt;/h3&gt; &lt;p&gt;Item 3&lt;/p&gt; &lt;h3&gt;Associated Items&lt;/h3&gt; &lt;p&gt;Item 4&lt;/p&gt; &lt;h3&gt;Associated Items&lt;/h3&gt; &lt;p&gt;Item 5&lt;/p&gt; </code></pre> <p>Is there a way to group the same nodes as an unordered list? Something like this, perhaps:</p> <pre><code>&lt;h3&gt;Name&lt;/h3&gt; &lt;p&gt;Item 1&lt;/p&gt; &lt;h3&gt;Description&lt;/h3&gt; &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/p&gt; &lt;h3&gt;Associated Items&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;Item 2&lt;/li&gt; &lt;li&gt;Item 3&lt;/li&gt; &lt;li&gt;Item 4&lt;/li&gt; &lt;li&gt;Item 5&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Thank you! (Please be nice.)</p> <p>PS The child nodes that repeat aren't limited to AssociatedTerms only. There are possibly more child nodes of the same name that repeat.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload