• 向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); //保存到文件

    这样就可以了~~~


  • 相关阅读:
    浏览器加载AMD标准的输出文件
    Mac安装brew && brew 安装yarn
    插件集
    vue-router复用组件时不刷新数据
    加入sass后运行项目报错:TypeError: this.getResolve is not a function
    安装cnpm后运行报cnpm : 无法加载文件 C:UsersyizonAppDataRoaming pmcnpm.ps1,因为在此系统上禁止运行脚本
    图片canvas跨域问题解决方案之一
    vscode配置
    搭建express服务
    项目初始化
  • 原文地址:https://www.cnblogs.com/cdts_change/p/1686869.html
Copyright © 2020-2023  润新知