11

I have XML String:

   <GroupBy Collapse=\"TRUE\" GroupLimit=\"30\">
      <FieldRef Name=\"Department\" />
   </GroupBy>
   <OrderBy>
      <FieldRef Name=\"Width\" />
   </OrderBy>

I am new in C#. I tried to read the Name attribute of the FieldRef element for both elements but I could not. I used XMLElement , is there any way to pick these two values?

1
  • 4
    What's the rest of the XML? Valid XML must have one root node. Commented Jan 17, 2012 at 2:01

2 Answers 2

13

Despite the posting of invalid XML (no root node), an easy way to iterate through the <FieldRef> elements is to use the XmlReader.ReadToFollowing method:

//Keep reading until there are no more FieldRef elements
while (reader.ReadToFollowing("FieldRef"))
{
    //Extract the value of the Name attribute
    string value = reader.GetAttribute("Name");
}

Of course a more flexible and fluent interface is provided by LINQ to XML, perhaps it would be easier to use that if available within the .NET framework you are targeting? The code then becomes:

using System.Xml.Linq;

//Reference to your document
XDocument doc = {document};

/*The collection will contain the attribute values (will only work if the elements
 are descendants and are not direct children of the root element*/
IEnumerable<string> names = doc.Root.Descendants("FieldRef").Select(e => e.Attribute("Name").Value);
Sign up to request clarification or add additional context in comments.

9 Comments

the problem is, this string is not fixed, some times it will not contains the GroupBy element, some times it will contains OrderBy element. I need to pick the FieldRef and I should know for which root element it belong to.
I noticed that the FieldRef element is not always contained within the same parent node, descendants will account for this and extract it irrespective of the element it is nested within. If you wish to also get the parent element using LINQ to XML, try this: IEnumerable<KeyValuePair<XElement, string>> pairings = doc.Root.Descendants("FieldRef").Select(e => new KeyValuePair<XElement, string>(e.Parent, e.Attribute("Name").Value));
I have a string not a file, how I will define doc varaible? as I see its XDocument type how can I link my string to doc?
@Waleed Sorry I didn't realise that, as long as the string is valid XML (which your posted XML is not, it requires a root node,) then you can use the XDocument doc = XDocument.Parse({xml}); static method, which is documented on MSDN
I am so sorry, but I have an error: 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' could be found (are you missing a using directive or an assembly reference?) Should i define e as XMLElement before? and what value should I give to it?
|
-1

try this:

    string xml = "<GroupBy Collapse=\"TRUE\" GroupLimit=\"30\"><FieldRef Name=\"Department\" /></GroupBy><OrderBy> <FieldRef Name=\"Width\" /></OrderBy>";
    xml = "<root>" + xml + "</root>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    foreach (XmlNode node in doc.GetElementsByTagName("FieldRef"))
        Console.WriteLine(node.Attributes["Name"].Value);

1 Comment

the problem is, this string is not fixed, some times it will not contains the GroupBy element, some times it will contains OrderBy element. I need to pick the FieldRef and I should know for which root element it belong to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.