简体   繁体   中英

XElement Nodes and child nodes

I'm sorry for advanced if this question has been repeated, but from my search there hasn't been any appropriate answers for my particular error.

So to the question, I've currently got an excel document with several rows of XML within the spread sheet. What I am currently doing is creating a console application within C# to then filter the XML in to readable data. I've currently able to extract the documents content and now I'm running through the code to break down the data.

So for the xml structure:

the xml has several fields, within each field there is a label and value fields. So I've taken the route to create a list of all the nodes (egfields) and try to extract the data with XElement. Also I know all the labels so what I will do is use the labels to try to collect the values.

foreach(DataRow row in result.Tables[0].Rows)
   {
       var XmlData = row.ItemArray[2].ToString();
       XElement doc = XElement.Parse(XmlData);
       foreach (XElement element in doc.Elements("fields"))
          {
              var userEntry = new FieldEntryModel();

              //element.Value.ToList();
              var elementNodes = element.Nodes().ToList();
              var nodeBreakdown = element.Nodes().OfType<XElement>();

              foreach (var something in nodeBreakdown)
                 {
                    var willthiswork = something.DescendantNodes().OfType<XElement>();                             
                    foreach (var item in willthiswork) {
                       if (item.Value != null)
                           {
                                if (item.Value.ToString().Contains("Forename"))
                                    //Console.WriteLine(item..ToString());
                                //userEntry.Forename = item.Value.ToString();
                             }
                    }                           
            }
    }

I start to get confused between the naming conversions and the object types to obtain the data.

SAMPLE XML:

<fields>
   <field>
      <label>
        Name
      <label>
      <value>
        Test
      </value>
   </field>
   <field>
      <label>
        job
      <label>
      <value>
        developer
      </value>
   </field>
   <field>
      <label>
        address
      <label>
      <value>
        1 Test
      </value>
   </field>
   <field>
      <label>
        Name
      <label>
      <value>
        Test
      </value>
   </field>
 </fields>

I would then store this data in my field entry model so that is:

 public class FieldEntryModel
{
    public string Name {get;set;}
    public string Job {get;set;}
    public string Address{get;set;}
}

I believe something like this may work for you. Since you seem to have duplicate labels in your fields (eg "<label>Name</label>" appears twice), I've used FirstOrDefault here. You should investigate why there are duplicates and handle appropriately. If there were no duplicates, SingleOrDefault would be the better choice.

var fieldElements = doc.Elements("fields").Elements("field");
var nameElement = fieldElements.FirstOrDefault(e => e.Element("label")?.Value == "Name");
var jobElement = fieldElements.FirstOrDefault(e => e.Element("label")?.Value == "job");
var addressElement = fieldElements.FirstOrDefault(e => e.Element("label")?.Value == "address");
var model = new FieldEntryModel
            {
                Name = nameElement?.Element("value")?.Value,
                Job = jobElement?.Element("value")?.Value,
                Address = addressElement?.Element("value")?.Value,
            };

Note that I've used the null-conditional operator from C# 6 (Visual Studio 2015+) in this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM