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.

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

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.

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.

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

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));
}
}
}

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();

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.

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?