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>

<propertyname="url"type="String"flags="0">/abc1/Samlple.xml</property>
<property name="icon" type="String" flags="0" />
</propertybag>

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,

Anjali....

Dani AI

Generated

Short note for (and following up on ): the XML in your post has a small well-formedness problem (the propertyname="url" line). Fix the XML first, then use any of the .NET XML APIs to find the property element whose name attribute equals "path" and read its text. For example, a corrected snippet:

<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="url" type="String" flags="0">/abc1/Sample.xml</property>
  <property name="icon" type="String" flags="0" />
</propertybag>

A simple, robust streaming approach (XmlReader) that stops as soon as it finds the path property:

using System;
using System.Xml;

string pathValue = null;
using (var r = XmlReader.Create("file.xml"))
{
    while (r.Read())
    {
        if (r.NodeType == XmlNodeType.Element && r.Name == "property"
            && string.Equals(r.GetAttribute("name"), "path", StringComparison.OrdinalIgnoreCase))
        {
            pathValue = r.ReadElementContentAsString().Trim('/');
            break;
        }
    }
}
// pathValue == "abc"

If you prefer in-memory querying (short and readable), use LINQ to XML (XDocument) and then Descendants("property") with a predicate checking the name attribute. Common pitfalls: remember XML is case-sensitive, ensure the file is well-formed, handle namespaces if present, and decide whether you need the whole path ("/abc1/Sample.xml"), the last segment ("Sample.xml"), or the trimmed value ("abc"). Use Trim('/'), Split('/'), or Path.GetFileName accordingly.

Recommended Answers

All 2 Replies

Read in System.XML.XMLReader and System.XML.XMLDocument classes

Read in System.XML.XMLReader and System.XML.XMLDocument classes

Ya , i have used those classes, but iam unable to fetch the perticular value by checking some conditions.......

iam able to read all the elements and values but i need to look for name = "path" and wants to fetch "/abc" value for that...

Please suggest me for this....

Thanks alot,
Anjali ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.