简体   繁体   中英

Adding or Updating XML Node

I need to either add a couple child nodes or update the node if it already exists. As you can see from the small sample below, I have one node with a child nodes, and another without. Say I have a definition coming from a SQL table for both of these, I want to add the value to the node.

XML

<workbook>
<datasources>
    <column datatype='real' name='[CONTRACT_PAYMENT_AMT]' role='measure' type='quantitative'>
        <desc>
          <formatted-text>
            <run>The amount being paid on the contract.</run>
          </formatted-text>
        </desc>
    </column>
    <column datatype='real' name='[CONTRACT_PAYMENT_DATE]' role='measure' type='quantitative'>
    </column>
</datasources>

Here is the code I have to just get the name attribute. Columns is the class that could potentially hold datatype, name, role, and type.

IEnumerable<string> names = from Columns in XDocument.Load(path).Descendants("column")                                        
                                    select Columns.Attribute("name").Value;

        foreach (string name in names)
        {

        }

This was intended to be a start to the method.

I am having trouble thinking about how I can either store the values and update the XML doc, or just look at each node maybe and update as it goes through the document. Any ideas?

EDIT - New code adding new value to the current Run node but not adding a new one..with the definition.

var document = XDocument.Load(path);
            var elements = (from column in document.Descendants("column")
                            select new
                                {
                                    AttributeName = column.Attribute("name").Value,
                                    Run = column.Descendants("run").FirstOrDefault() ?? new XElement("run")
                                }).ToList();

            foreach (var item in elements)
            {
                if (item.Run == null) continue;
                else 
                {
                    item.Run.Value = GetVariable(item.AttributeName);
                                     // Getvariable is the method that returns the definition
                }
            }

            document.Save(path);

You can dot the following logic:

var document = XDocument.Load(path);
var elements = (from column in document.Descendants("column")
               select new
               {
                    AttributeName = column.Attribute("name").Value,
                    Parent = column,
                    Run = column.Descendants("run").FirstOrDefault()
               }).ToList();

foreach(var item in elements)
{
     XElement run;
     if (item.Run == null)
     {
          run = new XElement("run");
          item.Parent.Add(
              new XElement("desc", 
                  new XElement("formatted-text", 
                      run
                  )
              )
          );
      }
      else
      {
           run = item.Run;
      }

      if(item.AttributeName == "BlaBlaOne")
      {
           run.Value = "Your definition here";
      }
 }

 document.Save(path);

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