SP1
SP1

Reputation: 1202

Get specific node from XElement using .net

I have the below xml. I only want the <Profile> element.

<Profiles>
<ProfileInfo>
<Profile>
<name>test</name>
<age>2</age>
</Profile>

</ProfileInfo>
</Profiles>

I tried doing

var nodes1 = nodes.Elements().Where(x => x.Element("Profiles") != null).ToList();
foreach (var node in nodes1)
    node.Remove();

I also tried to get the value directly

var nodes = xmlDocumentWithoutNs.Elements()
    .Where(x => x.Element("Profile") != null)
    .ToList();

But this doesn't get the data I want. What do I need to change to get the data I want?

I would like the result in this form (representation):

<Profile>
    <name>test</name>
    <age>2</age>
</Profile>

Upvotes: 1

Views: 4275

Answers (3)

jdweng
jdweng

Reputation: 34421

Try this using XML Linq. I this case it is easier to add the elemements to a new XML object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<Profiles>" +
                    "<ProfileInfo>" +
                    "<Profile>" +
                    "<name>test1</name>" +
                    "<age>2</age>" +
                    "</Profile>" +
                    "<Profile>" +
                    "<name>test2</name>" +
                    "<age>2</age>" +
                    "</Profile>" +
                    "<Profile>" +
                    "<name>test3</name>" +
                    "<age>2</age>" +
                    "</Profile>" +
                    "</ProfileInfo>" +
                    "</Profiles>";

            XElement element = XElement.Parse(input);

            XElement newElement = null;

            foreach (XElement profile in element.Descendants("Profile"))
            {
                if (newElement == null)
                {
                    newElement = profile;
                }
                else
                {
                    newElement.Add(profile);
                }
            }
        }
    }
}
​

Upvotes: 0

blogbydev
blogbydev

Reputation: 1485

This example might help:

XElement root = XElement.Parse(@"
    <Profiles>
        <ProfileInfo>
            <Profile>
            <id>5</id>
            </Profile>
        </ProfileInfo>
        <ProfileInfo>
            <Profile>
            <id>6</id>
            </Profile>
        </ProfileInfo>
    </Profiles>
");


var node2 = root.Elements("ProfileInfo").ToList();
Console.WriteLine (node2[0].Element("Profile").Element("id").Value.ToString());

Upvotes: 0

Volkan Paksoy
Volkan Paksoy

Reputation: 6937

The following snippet would get the value of the first child Profile element:

var someData = doc.Root.DescendantsAndSelf("Profile").First();

The value of someData would be:

<Profile>
  <name>test</name>
  <age>2</age>
</Profile>

Upvotes: 4

Related Questions