10 Replies - 3143 Views - Last Post: 03 October 2009 - 05:18 PM Rate Topic: -----

#1 Kattinti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 09

Linq to XML, How to grab the Tag names but not the values btwn them

Posted 01 October 2009 - 10:44 AM

Following is the example of an xml document

<?xml version="1.0" ?>
<People>
<Person>
<Name> ABC </Name>
<SSN> 111111 </SSN>
<Address> asdfg </address>
</Person>
</People>

I am working on the project where I should get the Tag names but not the values between the tag names. i.e., Under Person Tag, I should grab the Name, SSN, Address nodes but the ABC, 111111, asdfg values.

I need to use Linq to XML and query it in c#. But I have no clue how to proceed with this. I am a newbie, so please help me out with this issue.

Awaiting for replies.

Thanks & regards,

This post has been edited by Kattinti: 01 October 2009 - 10:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Linq to XML, How to grab the Tag names but not the values btwn them

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 01 October 2009 - 10:47 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 Kattinti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 09

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 01 October 2009 - 12:04 PM

View PostKattinti, on 1 Oct, 2009 - 09:44 AM, said:

Following is the example of an xml document

<?xml version="1.0" ?> 
<People>
   <Person>
	  <Name> ABC </Name>
	  <SSN> 111111 </SSN>
	  <Address> asdfg </address>
</Person>
</People>



I am working on the project where I should get the Tag names but not the values between the tag names. i.e., Under Person Tag, I should grab the Name, SSN, Address nodes but the ABC, 111111, asdfg values.

I need to use Linq to XML and query it in c#. But I have no clue how to proceed with this. I am a newbie, so please help me out with this issue.

Awaiting for replies.

Thanks & regards,



I am a newbie and I do not how to start with. I need a help in writing the query.

<People>
   <Person>
	  <Name> ABC </Name>
	  <SSN> 111111 </SSN>
	  <Address> asdfg </address>
</Person>
</People>




XDocument xmlDoc = Xdocument.Load(FileName);

var v = from c in xmlDoc.Descendants()
select c;
foreach (XElement v1 in v)

{
if (v1.Element("Person"))
{
foreach(XElement x in v1.Elements())
{
Console.WriteLine(x.Name);
}
}
}

But its gives me null reference excpetion. Please need some input on this. I am very new to this. I am having tough time getting this to work.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 01 October 2009 - 12:24 PM

I am using this XML file....

<?xml version="1.0" encoding="utf-8"?>
<ContactList>
  <Contact Name="Ryan">
    <Phone>145-8445</Phone>
  </Contact>
  <Contact Name="David">
    <Phone>987-2652</Phone>
  </Contact>
</ContactList>




XDocument doc = Xdocument.Load(@"C:\test\book.xml");

// gets a list of the main elements
var query = from c in doc.Root.Elements()
            select c;

// somewhere to store the names
List<string> elementNames = new List<string>();
List<string> attributeNames = new List<string>();

// loop for each of the main elements
foreach (var q in query)
{
    if (!elementNames.Contains(q.Name.ToString()))
        elementNames.Add(q.Name.ToString());

    // see if the element has any subelements
    if (q.HasElements)
    {
        var anotherQuery = from a in q.Elements()
                           select a;

        foreach (var a in anotherQuery)
        {
            if (!elementNames.Contains(a.Name.ToString()))
                elementNames.Add(a.Name.ToString());
        }
    }

    // see if element has any attributes
    if (q.HasAttributes)
    {
        var anotherQuery = from a in q.Attributes()
                           select a;

        foreach (var a in anotherQuery)
        {
            if (!attributeNames.Contains(a.Name.ToString()))
                attributeNames.Add(a.Name.ToString());
        }
    }
}

Console.Read();


This post has been edited by eclipsed4utoo: 01 October 2009 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 Kattinti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 09

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 01 October 2009 - 12:45 PM

Thanks for the help eclipsed4utoo.

This would help me to go further... Thanks for the help :^:
Was This Post Helpful? 0
  • +
  • -

#6 Kattinti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 09

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 01 October 2009 - 02:28 PM

Hello eclipsed4utoo.,

I tried working on the code. I was looking for a particular node and getting the children of that particular node. I took ur code as reference and have changed it to like this.. But it did not work. there were build errors saying that you cannot compare the string. Please throw some input on this. That would be of great help.

protected override void LoadSourceFields() {
		
		_sourceFields = new List<SourceFieldInfo>();
		XDocument doc = Xdocument.Load(FileName);

		// gets a list of the main elements
		var query = from c in doc.Root.Elements()
					select c;

		// somewhere to store the names
		List<string> elementNames = new List<string>();
		List<string> attributeNames = new List<string>();

		// loop for each of the main elements
		foreach (var q in query)
		{
			if (!elementNames.Contains(q.Name.ToString() == "Person"))
				elementNames.Add(q.Name.ToString());

			// see if the element has any subelements
			if (q.HasElements)
			{
				var anotherQuery = from a in q.Elements()
								   select a;

				foreach (var a in anotherQuery)
				{
					if (!elementNames.Contains(a.Name.ToString()))
						elementNames.Add(a.Name.ToString());
					//Console.WriteLine(elementNames);
					_sourceFields.Add(new SourceFieldInfo(a.Name.LocalName));
				}
			}

			// see if element has any attributes
			if (q.HasAttributes)
			{
				var anotherQuery = from a in q.Attributes()
								   select a;

				foreach (var a in anotherQuery)
				{
					if (!attributeNames.Contains(a.Name.ToString()))
						attributeNames.Add(a.Name.ToString());
					//Console.WriteLine(attributeNames);
					_sourceFields.Add(new SourceFieldInfo(a.Name.LocalName));
				}
			}
		}







I used another way also but it kind of not working...
var doc = Xdocument.Parse(@"<People>
   <Person>
	   <Name>ABC </Name>
	   <SSN>111111</SSN>
	   <Address>asdfg</Address>
   </Person>
</People>"
);

var list = doc.Root.Element("Person").Descendants()
			  .Select(node => node.Name.LocalName).ToList();


Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 02 October 2009 - 04:38 AM

this is not how you use the "Contains" method..

if (!elementNames.Contains(q.Name.ToString() == "Person"))
      elementNames.Add(q.Name.ToString());



in this case, Contains accepts a string argument. You are passing it a boolean argument since you are trying to compare q.Name and "Person".
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 02 October 2009 - 04:56 AM

I think I see what you are wanting to do. You just want to get the names of the elements under "Person", correct?

if so,

my XML file
<?xml version="1.0" encoding="utf-8"?>
<ContactList>
  <Contact Name="Ryan">
    <Address>123 Easy Street</Address>
    <City>New York</City>
    <State>New York</State>
    <Phone>145-8445</Phone>
  </Contact>
  <Contact Name="David">
    <Address>456 West Street</Address>
    <City>Los Angeles</City>
    <State>California</State>
    <Phone>987-2652</Phone>
  </Contact>
</ContactList>




code:
XDocument doc = Xdocument.Load(@"C:\test\book.xml");

// gets a list of the main elements
var query = from c in doc.Root.Descendants("Contact")
            select c;

// somewhere to store the names
List<string> elementNames = new List<string>();
List<string> attributeNames = new List<string>();

// loop for each of the main elements
foreach (var q in query)
{
    // see if the element has any subelements
    if (q.HasElements)
    {
        var anotherQuery = from a in q.Elements()
                           select a;

        foreach (var a in anotherQuery)
        {
            if (!elementNames.Contains(a.Name.ToString()))
                elementNames.Add(a.Name.ToString());
        }
    }

    // see if element has any attributes
    if (q.HasAttributes)
    {
        var anotherQuery = from a in q.Attributes()
                           select a;

        foreach (var a in anotherQuery)
        {
            if (!attributeNames.Contains(a.Name.ToString()))
                attributeNames.Add(a.Name.ToString());
        }
    }
}

Console.Read();



Does that work better?
Was This Post Helpful? 1
  • +
  • -

#9 Kattinti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 09

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 02 October 2009 - 02:35 PM

Yes it worked. Thanks so much.. I do not know if I can ask the another question right away or not but meanwhile I was trying some other stuff and I am stuck..

I just want to know how to navigate from one schema to another schema using linq to XML in c#. I used the same piece of code and parsed my xsd
XDocument doc = Xdocument.Load("xsd path------");

// gets a list of the main elements
var query = from c in doc.Root.Descendants("Person")
						select c;

// somewhere to store the names
List<string> elementNames = new List<string>();
List<string> attributeNames = new List<string>();

// loop for each of the main elements
foreach (var q in query)
{
if (!elementNames.Contains(q.Name.ToString()))
elementNames.Add(q.Name.ToString());

// see if the element has any subelements
if (q.HasElements)
{
var anotherQuery = from a in q.Elements()
select a;

foreach (var a in anotherQuery)
{
if (!elementNames.Contains(a.Name.ToString()))
elementNames.Add(a.Name.ToString());
}
}

// see if element has any attributes
if (q.HasAttributes)
{
var anotherQuery = from a in q.Attributes()
select a;

foreach (var a in anotherQuery)
{
if (!attributeNames.Contains(a.Name.ToString()))
attributeNames.Add(a.Name.ToString());
}
}
}

Console.Read();



But the person's definition is in some other schema. when i right click on the person in schema 1 it will take me to another schema 2 whcih has nodes for person like Name, SSN, Address. How to access these nodes using the linq to xml. I tried the above code it did not work for my xsd. If you need elaborate schema I can show u the schema in my next post.. But the schema is too huge to even post.

Thansk for the help in advance. I am stuck completely and blanked out.

Need help so that I can work further. I will try in the mean while

Thanks again eclipsed4utoo
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 03 October 2009 - 06:09 AM

as long as you are trying, you can ask as many questions as you like and we will try to help.

However, I am not quite understanding what you are trying to do now. When you say "in another schema", do you mean in another file? in another element? or am I totally missing what you are talking about?
Was This Post Helpful? 0
  • +
  • -

#11 Kattinti  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 09

Re: Linq to XML, How to grab the Tag names but not the values btwn them

Posted 03 October 2009 - 05:18 PM

Yes I meant another file. I have person in person.xsd and its nodes in PersonNodes.xsd.
How to navigate these two files using linq to xml in c#.

I need few examples on it so that I can get a hang of it.

I tried to check for an example or clue on how to take on this. I did not find any idea. Please throw your thoughts...

Thanks,
SK
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1