簡體   English   中英

在基於兄弟后代節點的節點的子節點屬性值中添加新的子節點

[英]add new child nodes in sibling descendant node based node's child node attribute value

以下是xml文件

 <al>
  <hfs>
    <hf id="1">A2</hf> ##1) based these hf nodes  id attributes
    <hf id="2">A1</hf>
  </hfs>
  <psteps>
    <pstep>
      <name>sharepoint</name>
      <lze>
        <lz hid="1">                          ##1) in Lze node need to create same number lz nodes 
                                              ## based previously quoted hf node id attribute        
        <ps>
         <p>
           <text>ziel</text>
           <inhalt>ttt</inhalt>    
         </p> 
          <p>
           <text></text>
           <inhalt></inhalt>
         </p> 
        </ps>
        </lz>
        <lz hid="2">
        <ps>         
         <p>
           <text></text>
           <inhalt></inhalt>                    ##2) lz node with hid = "2" has 2 p nodes
         </p>
         <p>
           <text></text>
           <inhalt></inhalt>
         </p>      
        </ps>
        </lz>
      </lze>
    </pstep>
      <pstep>
      <name>aspnet</name>
      <lze>
        <lz hid="2">                    ## 1)in Lze node need to create same number lz nodes 
                                         ## based previously quoted hf node id attribute 
       <ps>
         <p>
           <text>ziel</text>                 ## 2)lz node with hid = "2" has 1 p node so need 
           <inhalt>ttt</inhalt>              ## node to create 1 more  as previously 
         </p>                                ## lz node
        </ps>
        </lz>      
      </lze>
    </pstep>
  </psteps> 
 </al>

我需要在這里做兩件事:

  1. 基於hfs子hf節點,我需要在hfs同級psteps后代lze節點中創建相同數量的具有相同屬性值的lz節點

  2. 接下來必須在具有相同屬性值的lz節點中找到最大數量的p個節點,並在具有相同屬性值的其余lz節點中創建相同數量的p個節點。

這是我嘗試生成的示例xml:

 <al>
  <hfs>
    <hf id="1">A2</hf>
    <hf id="2">A1</hf>
  </hfs>
  <psteps>
    <pstep>
      <name>sharepoint</name>
      <lze>
        <lz hid="1">
        <ps>
         <p>
           <text>ziel</text>
           <inhalt>ttt</inhalt>
         </p> 
          <p>
           <text></text>
           <inhalt></inhalt>
         </p> 
        </ps>
        </lz>
        <lz hid="2">
        <ps>         
          <p>
           <text></text>
           <inhalt></inhalt>
         </p>
         <p>
           <text></text>
           <inhalt></inhalt>
        </p>         
        </ps>
        </lz>
      </lze>
    </pstep>
      <pstep>
      <name>aspnet</name>
      <lze>
        <lz hid="2">
        <ps>
         <p>
           <text>ziel</text>
           <inhalt>ttt</inhalt>
         </p>
         <p>
           <text></text>
           <inhalt></inhalt>
         </p>           
        </ps>
        </lz>      
       <lz hid="1">
        <ps>
         <p>
           <text></text>
           <inhalt></inhalt>
         </p>      
         <p>
           <text></text>
           <inhalt></inhalt>
         </p>          
        </ps>
        </lz>      
      </lze>
    </pstep>
  </psteps> 
 <al>

我使用以下代碼能夠創建缺少的<lz>節點。 我需要創建<p>節點,無法從每個<lz>節點中選擇<p> <lz>節點。

var doc = XDocument.Load("XmlFile1.xml");
var hfIds = (from hf in doc.Descendants("hf")
                from attr in hf.Attributes("Id")
                select attr.Value).Distinct(StringComparer.Ordinal).ToList();

var lze2 = doc.Descendants("lze")
        .Select(lze => new {
            element = lze,
            hfids = lze.Descendants("lz").Attributes("hid"),
            paras = (from lz in lze.Elements("lz")
                    from ps in lz.Elements("ps")
                    from p in ps.Elements("p")
                    select p).ToList()
        });

foreach (var c in lze2) {
    foreach (var hfid in hfIds.Where(hfid => !c.hfids.Any(attr => hfid.Equals(attr.Value)))) {
        c.element.Add(new XElement("lz", new XAttribute("hfid", hfid),
            new XElement("ps",
                    new XElement("p"),
                new XElement("Text"),
                new XElement("Content"))));
        break;
    }
}

謝謝您的幫助

這是您想要做的嗎?

var doc = XDocument.Load("XmlFile1.xml");
var hfIds = (from hf in doc.Descendants("hf").Attributes("id")
                         select hf.Value).Distinct(StringComparer.Ordinal).ToList();


            bool addFlag = true;

            foreach (var c in doc.Descendants("lze"))
            {
                foreach (var hfid in hfIds)
                {
                    addFlag = true;
                    foreach (var attr in c.Descendants("lz").Attributes("hid"))
                    {
                        if (hfid == attr.Value)
                        {
                            addFlag = false;
                            break;
                        }

                    }
                    if (addFlag)
                    {
                        c.Add(new XElement("lz", new XAttribute("hid", hfid),
                            new XElement("ps",
                                    new XElement("p"),
                                new XElement("Text"),
                                new XElement("Content"))));
                    }
                }
            }


            // Adding the p elements to the above added lz elements
            var distinctPIds = (from hf in doc.Descendants("lz").Descendants("p")
                                select hf).GroupBy(x => x.Value).Select(x => x.First()); ;

            addFlag = true;
            XElement a = null;
            foreach (var c in doc.Descendants("lz").Descendants("ps"))
            {
                foreach (var c3 in distinctPIds)
                {
                    addFlag = true;
                    foreach (var c2 in c.Descendants("p"))
                    {
                        if (XNode.DeepEquals(c2, c3))
                        {
                            addFlag = false;
                            break;
                        }
                        a = c3;
                    }
                    if (addFlag)
                        c.Add(a);
                }
            }
        }

代碼可能不是最佳格式。 保持口頭理解。

 if (xmlDoc.Descendants("a").Count() > 0)
   {
     var afIds = (from af in xmlDoc.Descendants("af")
                 from attribute in af.Attributes("Id")
                 select attribute.Value).Distinct(StringComparer.Ordinal).ToList();

     var loContainers = xmlDoc.Descendants("a").Select(a => 
                    new {element = a, afids = a.Descendants("x").Attributes("afId") });

     foreach (var container in loContainers)
     {
       foreach (var afId in afIds.Where(afId => !container.afids.Any(attr => afId.Equals(attr.Value))))
       {
          container.element.Add(new XElement("x", new XAttribute("afId", afId),
                            new XElement("Paragraphs",
                                new XElement("Paragraph",
                                    new XAttribute("AllowSelection", "false"),
                                    new XElement("Text"),
                                    new XElement("Content")))));
             break;
         }
      }

      foreach (var afId in afIds)
      {
        var distinctlearningObjs = from x in xmlDoc.Descendants("x").Where(
                            x => (string)x.Attribute("afId").Value == afId).GroupBy(
                                x => x.Attribute("afId").Value)
                      select new { MaxParaCount = x.Max(y => y.Descendants("Paragraph").Count ()) };

         foreach (var x in distinctlearningObjs)
         {
            var docLos = from doclo in xmlDoc.Descendants("x").Where(
                                x => (string)x.Attribute("afId").Value == afId)
                        select new { doclo,  element = doclo.Element("Paragraphs"),
                                    count = doclo.Descendants("Paragraph").Count()
                        };

             foreach (var docLo in docLos)
             {
                var paraCount = docLo.count;
                var maxCount = x.MaxParaCount;

                for (var i = paraCount; i < maxCount; i++)
                {
                   docLo.element.Add(new XElement("Paragraph",
                                    new XAttribute("AllowSelection", "false"),
                                    new XElement("Text"),
                                    new XElement("Content")));
                 }
              }
           }
         }
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM