0

I have a xmlNodeList as

<Fields>
  <Field FieldId="1" Value="123" FieldTitle="id" FieldType="Text"/>
  <Field FieldId="2" Value="abc" FieldTitle="First Name" FieldType="Text"/>
  <Field FieldId="3" Value="efg" FieldTitle="Last Name" FieldType="Text"/>
</Fields>

now what I want is

var id        = 123   //select the `value` if `FieldId == "1";
var firstName = abc   //select the `value` if `FieldId == "2";
var last name = efg  //select the `value` if `FieldId == "3";

Edit: I don't want to loop threw the fields and check every field with if condition.

One liner solution is most welcome.

NOTE: I am dealing with very large XML and the Fields is a part of single node and there are about 500 fields of thousand of nodes, any other better solution to convert this much larger XML file into insert queries is most welcome

1 Answer 1

1

You can have fairly good idea, how to get it done by using this piece of code:

XDocument doc = XDocument.Load(@"XmlFile1.xml");
var elem = doc.Descendants("Field");

var id = elem.Where(c => c.Attribute("FieldId").Value.Equals("1")).Select(s => s.Attribute("Value").Value).FirstOrDefault();
var firstName = elem.Where(c => c.Attribute("FieldId").Value.Equals("2")).Select(s => s.Attribute("Value").Value).FirstOrDefault();
var lastName = elem.Where(c => c.Attribute("FieldId").Value.Equals("3")).Select(s => s.Attribute("Value").Value).FirstOrDefault();

One liner solution is not really possible as there is no way to segregate data between id, firstName, and lastName in one line of code.

ADDED:

var result = doc.Descendants("Field").Select(s => new { Field = s.Parent.GetHashCode(), FieldId = s.Attribute("FieldId").Value, Value = s.Attribute("Value").Value });
foreach (var val in result.GroupBy(g => g.Field).Select(s => s))
{
    var id = result.Where(c => c.Field == val.Key && c.FieldId == "1").Select(s => s.Value).FirstOrDefault();
    var firstName = result.Where(c => c.Field == val.Key && c.FieldId == "2").Select(s => s.Value).FirstOrDefault();
    var lastName = result.Where(c => c.Field == val.Key && c.FieldId == "3").Select(s => s.Value).FirstOrDefault();
    // ... do something ...
}

Hope this newly added code would give you some better idea :)

Sign up to request clarification or add additional context in comments.

2 Comments

I have xmlnode compleat xml is very large and is like leads\lead[]\fields\fields[] . after parsing xml i am in the loop and having xml nodelist as lead(list of leads) now from every lead i need all the fields. your solution is good but can you modify it to work with nodelist i like the methord elem.Where(c => c.Attribute("FieldId").Value.Equals("1")).Select(s => s.Attribute("Value").Value).FirstOrDefault(); but its not working with a xmlNode object
Can you modify you question with little Xml of leads\lead[]\fields\fields[] and try explaining it a little to help me understand you requirement in detail.

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.