简体   繁体   English

添加或更新XML节点

[英]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. 假设我有一个来自SQL表的定义,我想将值添加到节点。

XML 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. 这是我只需要获取name属性的代码。 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. 我在思考如何存储值并更新XML文档时遇到麻烦,或者仅查看每个节点并在文档中进行更新时遇到麻烦。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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