How to get XML attribute value from smart tag e.g '<thistag value = "xyz" />'

Hi all, 

I am still new in C# xml progamming.... 

I will like to retrieve the values of an xml tag attibutes with multiple start/abrreviated tags..... 

 E.g..... My string XML looks like  the following..... : 

<root>

 <persons>

   <person name = 'name1' age='21'/>

   <person name = 'name2 ' age='22'/> 

   <person name = 'name3' age='23'/> 

 </persons>

</root>

 

I want to loop through all person tag and  retrieve the name and the age....

Your help will be highly appreciated... 

Thanks in advance....

 

0
lekoya
11/5/2008 6:40:00 AM
asp.net.xml-datasource 7181 articles. 0 followers. Follow

3 Replies
827 Views

Similar Articles

[PageSpeed] 50

This should work for you:

            XmlDocument xmlPersons = new XmlDocument();
            xmlPersons.LoadXml(strXml);
            XmlNodeList xmlPersonsList = xmlPersons.DocumentElement.SelectNodes("//persons/person");
            foreach (XmlNode xmlNodePerson in xmlPersonsList)
            {
                string name = xmlNodePerson.Attributes["name"].Value;
                int age = int.Parse(xmlNodePerson.Attributes["age"].Value);
            }

 Just add some checks if the attributes are there and if the age can be cast to int.

 

Let me know if this answers your question.


Kung Fu developer


My Blog
0
donchevp
11/5/2008 7:29:30 AM

 at first create  load   XmlDocument  object

   XmlDocument xdoc = new XmlDocument();

   if your xml is a string

xdoc.LoadXml(XMLString);

 else

 xdoc.Load(filepath);

  XmlNodeList xnodelist = xdoc.DocumentElement.SelectNodes("persons/person");

  Dictionary<string, string> personrec= new Dictionary<string, string>();

 foreach (XmlNode xnode in xnodelist)
                            {
                                personrec.Add(xnode.Attributes.Item[0].InnerText, xnode.Attributes.Item[1].InnerText);
                               
                            }

 

 

 

 

 

 

0
pari_194
11/5/2008 7:39:27 AM

HI


try this

           XmlDocument xDoc = new XmlDocument();

            string name= "";

            string age   = "";


            xDoc.Load(HttpContext.Current.Server.MapPath("test.xml"));

            XmlNodeList xList = xDoc.SelectNodes("/persons/person");

            foreach (XmlNode xn in xList)
            {
                XmlElement element = xn as XmlElement;

                name= element.GetAttribute("name");

                age = element.GetAttribute("age");    //you can convert this age to integer if you want
            }

 


Sincerely,
MOONCODER

Please remember to click Mark as Answer on the post that helps you, and to click Unmark as Answer if a marked post does not actually answer your question.
0
mooncoder
11/5/2008 7:40:21 AM
Reply:

Similar Artilces:

Can't get away from: "Could not open in Design View. Quote values differently inside a '<% ... "value" ... %>' block."
I keep getting this error message when I try to switch to design view. This is the code that is causing it. OnClick="javascript:void(0);ToggleRowDisplay(this,'row<%# DataBinder.Eval(Container.DataItem, "Equipment_ID") %>');" When I take out this section I can go into design view without a problem. Any ideas?? is the word row has to be there?mess with the best...die like the rest...

"Could not open in Design View. Quote values differently inside a '<% ... "value" ... %>' block."
asp.net 1.1 vs.net 2003 i am working on someone else's asp.net project ... (and not sure under what conditions it was created) for many of the .aspx pages , when i attempt to view in "design" mode, i receive message "Could not open in Design View. Quote values differently inside a '<% ... "value" ... %>' block." what could be going on? Typically this happens when you have double quotes inside double quotes. Try to use double quotes outside and single quotes inside. Also, if you have <% %> as an attribute value, try to enclose it in...

quote values differently inside a '<% ..."value" ...%>' block
Hi,I am using a databound ScheduleGeneral control, The hyperlink (shown in red) works fine for normal data,but if my data contains single quot(') within it, then the url eliminates charaters after that quot. For example, if Program= The Planet's Animals, then it returns "MaterialLib.aspx?id=The Planet" If I replace the outer enclosing quotes(ie.<a href='mycode') with double quote("), it works fine, but I get the error message "values differently inside a '<% ..."value" ...%>' block .Please HELP! Thanks in advance. **********************************...

Could not open in design view. Quote values differently inside a '<%..."value"...%>' block
All i am trying to do is open the flash object inside an aspx page, but trying to pass the session("username") to it. I have the code and when i try to go into the design mode of my aspx form, it says the following: Could not open in design view. Quote values differently inside a '<%..."value"...%>' block <PARAM NAME="Movie" VALUE="GroupChat/GroupEngine.swf?LoginName=<%=Session("username")%>"><PARAM NAME="Src" VALUE="GroupChat/GroupEngine.swf?LoginName=<%=Session("username")%>"> Can you please tell me how can i pass my session("username") to the flash object....

Cound not open in design view.Quote values differently inside a '<% .."value" ...%>' block ????
I have datagrid with :<asp:TemplateColumn>      <ItemTemplate>       <a class=linkgray href="javascript:var win=open('AskItemID.aspx?ItemId=<%# Container.DataItem("ItemID")%>','_blank','status=yes,scrollbars=no,resizable=0')">Edit</a>      </ItemTemplate></asp:TemplateColumn>It occur above error message when i move to design mode ,not HTML modeCound not open in design view.Quote values differently inside a '<% .."value" ...%>' block ????How can i solv...

Error: Could not open in Design view. Quote values differently inside a '<%.."value"...%>'block.
I have this code for my datagrid ----------------------------------------------- <asp:TemplateColumn HeaderText="Title" Visible="true" runat="server" SortExpression="title" > <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "title") %> </ItemTemplate> </asp:TemplateColumn> ------------------------------------------------------------------------- it just work fine and compile no error... but when I want to swtich from HTML view to Design View,...

Why Response.Write("<script>window.open("newWind.aspx','report','');</script>") not working?
I need to open a new web browser of url=newWind.aspx and tried the code below; Response.Write("<script>window.open('newWind.aspx','report','');</script>")However nothing happens, what can I do to make it work? Try RegisterClientScriptBlock("OpenWin","<script language=javascript>window.open('newWind.aspx','report','');</script>")I think that is the name of it...Also look at RegisterStartupScriptBlock (I think)MajorCats Thanks. But, it was same difference. Is there anything that blocks the new browser from showing up in the setting of IE or system?   &nbs...

Give access to non virtual folder through configuration entry? ERROR: <location> path attribute must be a relative virtual path. It cannot contain any of '?' ':' '\' '*' '"' '<' '>' or '|'.
I have a webpage running under a virtual directory that is mapped to d:/webapplication that produces an excel spreadsheet, the page calls a library object that generates a report and places it in a folder d:/attachments. This folder is not in the virtual directory. When I access the webpage i click a button and it attemps to return the created spreadsheet as a mim content type application/vnd.ms-excel. However i getting redirected back to the login page of the application. Ive also noticed that the excel spreadsheet has not even been created at this location. So assuming this...

How to escape '<' character in "string" of appSettings value="string'?
Pleae help. I am trying to figure out how to escape '<' and '>' in the string of the value attribute of appSetting element in the web.config file.Thanks!Bernie Try &lt; and &gt;Darrell Norton, MVPDarrell Norton's BlogPlease mark this post as answered if it helped you! Thanks! I got it right this time. BernieThanks!Bernie...

superreview granted: [Bug 349705] In <wizard.xml>, Strict "Warning: function cancel does not always return a value" : [Attachment 234985] (Av1-XPFE) <wizard.xml>
neil@parkwaycc.co.uk <neil@httl.net> has granted Serge Gautherie <sgautherie.bz@free.fr>'s request for superreview: Bug 349705: In <wizard.xml>, Strict "Warning: function cancel does not always return a value" https://bugzilla.mozilla.org/show_bug.cgi?id=349705 Attachment 234985: (Av1-XPFE) <wizard.xml> https://bugzilla.mozilla.org/attachment.cgi?id=234985&action=edit ...

superreview requested: [Bug 349705] In <wizard.xml>, Strict "Warning: function cancel does not always return a value" : [Attachment 234985] (Av1-XPFE) <wizard.xml>
Serge Gautherie <sgautherie.bz@free.fr> has asked neil@parkwaycc.co.uk <neil@httl.net> for superreview: Bug 349705: In <wizard.xml>, Strict "Warning: function cancel does not always return a value" https://bugzilla.mozilla.org/show_bug.cgi?id=349705 Attachment 234985: (Av1-XPFE) <wizard.xml> https://bugzilla.mozilla.org/attachment.cgi?id=234985&action=edit ...

superreview requested: [Bug 449291] In <TestEncoding.cpp>, multiple "warning C4309: 'initializing' : truncation of constant value" : [Attachment 333054] (Av2) <TestEncoding.cpp>
Serge Gautherie <sgautherie.bz@free.fr> has asked Benjamin Smedberg [:bs] (bsmedberg) <benjamin@smedbergs.us> for superreview: Bug 449291: In <TestEncoding.cpp>, multiple "warning C4309: 'initializing' : truncation of constant value" https://bugzilla.mozilla.org/show_bug.cgi?id=449291 Attachment 333054: (Av2) <TestEncoding.cpp> https://bugzilla.mozilla.org/attachment.cgi?id=333054&action=edit ...

superreview canceled: [Bug 449291] In <TestEncoding.cpp>, multiple "warning C4309: 'initializing' : truncation of constant value" : [Attachment 333054] (Av2) <TestEncoding.cpp>
Benjamin Smedberg [:bs] (bsmedberg) <benjamin@smedbergs.us> has canceled Serge Gautherie <sgautherie.bz@free.fr>'s request for superreview: Bug 449291: In <TestEncoding.cpp>, multiple "warning C4309: 'initializing' : truncation of constant value" https://bugzilla.mozilla.org/show_bug.cgi?id=449291 Attachment 333054: (Av2) <TestEncoding.cpp> https://bugzilla.mozilla.org/attachment.cgi?id=333054&action=edit ------- Additional Comments from Benjamin Smedberg [:bs] (bsmedberg) <benjamin@smedbergs.us> I'm not sure I understa...

I'm getting an error using NavigateURL ='<%# Eval("Url") %>', it works when using NavigateURL="intro.aspx"
I'm getting an error using NavigateURL ='<%# Eval("Url") %>', it works when I use NavigateURL="intro.aspx" Server Error in '/orthoassist' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. Requested URL: /orthoassist/intro.aspx <asp:HyperLink runat="server" ID="hl1" Text=...

Web resources about - How to get XML attribute value from smart tag e.g '<thistag value = "xyz" />' - asp.net.xml-datasource

Resources last updated: 12/9/2015 6:02:49 AM