The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 7 of 7

Thread: Parsing XML file using C#

  1. #1

    Parsing XML file using C#

    Hello ,

    I am new to .net(C#) programing.
    I need some help in my coding , from experts.
    I have the following Xml file,
    Ex :
    <propertybag name="test" type="a" flags="0">
    <property name="help" type="String" flags="0" />
    <property name="path" type="String" flags="0">/abc</property>
    <property name="icon" type="String" flags="0" />

    Here i want to parse the Xml file till property name = "path" and wants to fetch the "abc" value from that element and same for bellow line.

    Please help me out of this, i got stucked here....
    i can tell that what method i used..
    Expecting your precious reply !!!

    Thanks in advance,

  2. #2
    I'm nice and I quoted him instead of just copying and pasting the links.

    Also there is a programming forum a few down, which this thread will be moved to shortly.

    Join #kirupa | Click the under my Username | CL | Jeff

  3. #3


    Ya i have gone through the links posted by Kirupa, but thats not specific to what i was looking for. ... need some suggetions for the same.

    Thanks ,
    Quote Originally Posted by Templarian View Post
    I'm nice and I quoted him instead of just copying and pasting the links.

    Also there is a programming forum a few down, which this thread will be moved to shortly.

  4. #4
    The links posted do cover what you are looking for. They may not do exactly what you are doing, but you can easily extend that information to cover your case.

    Basically, loop through your values until you find the attribute value "path", and once you do find it, get the innertext of that node.

    Great, now even Kirupa is { facebooking | twittering }

  5. #5


    Ya , i had tried for the same thing. But iam getting some error for this bellow mentioned code.
    "Index & length must reffer to a location within the string . parameter name:length" - Error1
    " Object reference not set to an instance of an object" - Error2

    Here 'Xnodes' is nodes for "propertybag"

    for (int i = 0; i <= Xnodes.Count - 1; i++)
    string Xname = Xnodes[i].ParentNode.Attributes["name"].Value;
    if (Xname.Equals("test"))
    XmlNode Xn = Xnodes[i];
    if (Xn.NextSibling.HasChildNodes)
    string ab = null;
    ab = Xn.InnerXml.Substring(1);

    textBox1.Text = ab.ToString();
    if (Xn.HasChildNodes)
    string a = null;
    a = Xn.InnerXml.Substring(1,4);
    string Xtext = Xn.InnerXml.Substring(6);
    textBox2.Text = a.ToString();
    textBox3.Text = Xtext.ToString();
    Please, suggest me inorder to remove the errors.
    Thanks a lot,

  6. #6
    i see your problem. XNode is for query based xml reading. That sounds weird, but that is what it is. The examples are for XmlNode. XNode is a linq node type. I am not sure why you need to use XNode, if you dont use a single linq expression, then i strongly suggest using XmlReader. For XNode, you will want something like:

    (sorry, i tried to fix your code, but its hella confusing)

    XDocument doc = new XDocument();
    XElement node = doc.Descendants(XName.Get("property")).Where(n=>n.Attribute(XName.Get("name")=="path").FirstOrDefault();
    what this will do is create a XDocument (so you need to load it up with the xml document through a file or whatever), then it goes to all its children with the node name "property" and then select only the ones with an attribute who's name is equal to "path". then assigns it to the XElement which then can produce a value. The first or default will either get the first record that has that path value, or return null. If there are multiple, it will ignore anything beyond the first. Also this code will search as deep as it needs to go, not just the children, i can't remember which item on XDocument is children only. "Decedents" means all nodes inside the element.

    also, when working with XDocument / XElement / XNode make sure that you use element over node, they are basically the same, only XNode sucks. (imo)

    edit:your error is basically saying you are trying to substring (most likely that inner xml) when there is nothing to substring off of. The string is empty or null and you are indexing on it.
    Last edited by bluefootedpig; March 31st, 2008 at 02:04 PM. Reason: explaining the error

  7. #7


    k fine.
    ya , i found that iam using complecated logic.....
    Thanks alot for your help........


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012