向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>
<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>
<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); //保存到文件
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); //保存到文件
这样就可以了~~~