Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2006

Tip: Looking for answers? Try searching our database.

how to get value of XML tag attribute in C#?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Saurabh - 29 Sep 2006 08:08 GMT
Hi,
my xml is:
<ROOT>
   <FILE NAME="filename">CDATA</FILE>
</ROOT>

this xml is argument to a function in C#.
i want to get the filename in a string variable.
how do i do it???
GetElementByTagname("FILE)[0].Attribute retrieves attributelist
how do i get the value of attribute????
Daniel - 29 Sep 2006 10:03 GMT
like this:

XmlTextReader tr = new XmlTextReader(filename);
while (tr.Read())
{
    if (tr.NodeType == XmlNodeType.Element)
    {
         switch (tr.LocalName)
         {

              case ("FILENAME"):
                  _fileName= int.Parse(tr.ReadString());
                  break;
         }
    }
}

but your xml would need to look like this:

<ROOT>
   <FILENAME>CDATA</FILENAME>
</ROOT>

that would do it.

> Hi,
> my xml is:
[quoted text clipped - 7 lines]
> GetElementByTagname("FILE)[0].Attribute retrieves attributelist
> how do i get the value of attribute????
Saurabh - 30 Sep 2006 07:49 GMT
Thanks Daniel, i learned a new way to access XML.
The problem is that the xml has to look like:
<ROOT>
   <FILE NAME="filename"></FILE>
</ROOT>

beacuse the xml is generated by a recursive function which scans the
file system (DirectoryInfo ans FileInfo class) and generate a xml. The
exact xxml format is:
<ROOT>
   <DIRECTORY>
       <FILE NAME="filename">CDATA contains filestream</FILE>
       <FILE NAME="filename">CDATA contains filestream</FILE>
   </DIRECTORY>
   <DIRECTORY>
       <FILE NAME="filename">CDATA contains filestream</FILE>
       <FILE NAME="filename">CDATA contains filestream</FILE>
   </DIRECTORY>
</ROOT>

you never know if the filename can be same with different extension.
just to be precautious i have made the file tag with filename as
attribute.
GVN - 30 Sep 2006 12:56 GMT
Hi Saurabh,

I hope the following code should help you.

XmlDocument _Document = new XmlDocument();
XmlNodeList properties;
_Document.Load (XmlFileName);
XmlNodeList properties = _Document["Design"].ChildNodes;
foreach (XmlNode property in properties)
{
    if(property.HasChildNodes)
    {
        XmlNodeList childNodes = property.ChildNodes;
        foreach(XmlNode childNode in childNodes)
        {
         MessageBox.Show(childNode.Attributes["FILE"].Value); // You can
show file name.
        }
    }
}

Thanks,
Muralidhar GVN

> Thanks Daniel, i learned a new way to access XML.
> The problem is that the xml has to look like:
[quoted text clipped - 19 lines]
> just to be precautious i have made the file tag with filename as
> attribute.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.