zoukankan      html  css  js  c++  java
  • 向XML插入节点

    假如现在有一个Xml文件,内容如下:

    <ReportItems>
      
    <Line Name="line2">
        
    <Top>3.75cm</Top>
        
    <Width>0.2381cm</Width>
        
    <Style>
          
    <BorderStyle>
            
    <Default>Solid</Default>
          
    </BorderStyle>
          
    <FontFamily>宋体</FontFamily>
        
    </Style>
        
    <ZIndex>2</ZIndex>
        
    <Left>3.75cm</Left>
        
    <Height>0.50265cm</Height>
      
    </Line>
    </ReportItems>

     

    现需要向下面的Xml文件内容的ReportItems节点下添加内容,变成下面的样子:

    <ReportItems>
      
    <Textbox Name="textbox1">
        
    <Top>1.5cm</Top>
        
    <Width>2.75cm</Width>
        
    <Style>
          
    <FontFamily>宋体</FontFamily>
          
    <PaddingLeft>2pt</PaddingLeft>
          
    <PaddingRight>2pt</PaddingRight>
          
    <PaddingTop>2pt</PaddingTop>
          
    <PaddingBottom>2pt</PaddingBottom>
        
    </Style>
        
    <ZIndex>4</ZIndex>
        
    <CanGrow>true</CanGrow>
        
    <Left>20.25cm</Left>
        
    <Height>2.25cm</Height>
        
    <Value>Hello World</Value>
      
    </Textbox>
      
    <Line Name="line1">
        
    <Top>3.75cm</Top>
        
    <Width>0.2381cm</Width>
        
    <Style>
          
    <BorderStyle>
            
    <Default>Solid</Default>
          
    </BorderStyle>
          
    <FontFamily>宋体</FontFamily>
        
    </Style>
        
    <ZIndex>2</ZIndex>
        
    <Left>3.75cm</Left>
        
    <Height>0.50265cm</Height>
      
    </Line>
    </ReportItems> 

    转换代码如下:

    string file = File.ReadAllText(filePath); //filePath是Xml存放的完整路径
    TextReader textReader = new StringReader(file);
    XElement doc 
    = XElement.Load(textReader);
    XElement node 
    = doc.Descendants().Where(c => c.Name.LocalName == "ReportItems").First(); //Name是包含命名空间的,LocalName不包含
    XNamespace ns = doc.Name.NamespaceName;
    XElement e 
    = new XElement(ns + "Textbox"new XAttribute("Name""textbox1"), //为节点添加属性
        new XElement(ns + "Top""1.5cm"), //在每个节点的名称前都要加上命名空间,不然会有xmlns:""的属性出现
        new XElement(ns + "Width""2.75cm"),
        
    new XElement(ns + "Style",
            
    new XElement(ns + "FontFamily""宋体"),
            
    new XElement(ns + "PaddingLeft""2pt"),
            
    new XElement(ns + "PaddingRight""2pt"),
            
    new XElement(ns + "PaddingTop""2pt"),
            
    new XElement(ns + "PaddingBottom""2pt"),
            
    new XElement(ns + "Color""#FFFFFF"),
            
    new XElement(ns + "BackgroundColor""#3F2FAF"),
            
    new XElement(ns + "FontSize""5pt")),
         
    new XElement(ns + "ZIndex""4"),
         
    new XElement(ns + "CanGrow""true"),
         
    new XElement(ns + "Left""20.25cm"),
         
    new XElement(ns + "Height""2.25cm"),
         
    new XElement(ns + "Value""Hello World"));

    node.Add(e); 
    //追加构造好的节点
    doc.Save(filePath); //保存到文件

    这样就可以了~~~


  • 相关阅读:
    GTD180007:【运维】LINUX学习
    GTD180006:【运维】安装调试GDB
    {done}GTD180005:【翻译】LISP prehistory
    ComPiler180001:【学习】编译器学习链接
    AIIE180002:AIIE2015大会主题
    AIIE180001:AIIE2016大会主题
    GTD180004:【开发】python_med
    GTD180003:【开发】python_oeis
    欧亚大帝国及一战、二战
    大洲分界线
  • 原文地址:https://www.cnblogs.com/cdts_change/p/1686869.html
Copyright © 2011-2022 走看看