在XML文件中有一部分如下:
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Unsynced/>
......
</WorksheetOptions>
我现在想删除<Unsynced/>
然后加上<PageSetup>的设置,想达到的效果如下:
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.51181102362204722"/>
<Footer x:Margin="0.51181102362204722"/>
<PageMargins x:Bottom="0.98425196850393704" x:Left="0.78740157480314965"
x:Right="0.47244094488188981" x:Top="0.98425196850393704"/>
......
</WorksheetOptions>
现在向大家请教,谢谢帮助!
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Unsynced/>
......
</WorksheetOptions>
我现在想删除<Unsynced/>
然后加上<PageSetup>的设置,想达到的效果如下:
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.51181102362204722"/>
<Footer x:Margin="0.51181102362204722"/>
<PageMargins x:Bottom="0.98425196850393704" x:Left="0.78740157480314965"
x:Right="0.47244094488188981" x:Top="0.98425196850393704"/>
......
</WorksheetOptions>
现在向大家请教,谢谢帮助!
xmlDoc.Load("你的文件名.xml");
XmlNodeList xnl = xmlDoc.SelectSingleNode("WorksheetOptions").ChildNodes;
foreach (XmlNode xn in xnl)//先删除
{
XmlElement xe = (XmlElement)xn;
if (xe.Name== "Unsynced")
{
xe.RemoveAll();
}
}
xmlDoc.Save("你的文件名.xml");//再增加节点
XmlNode root = xmlDoc.SelectSingleNode("WorksheetOptions");//查找<WorksheetOptions>
XmlElement xe1 = xmlDoc.CreateElement("PageSetup");//创建一个<PageSetup>节点 XmlElement xesub1 = xmlDoc.CreateElement("Header");
xesub1.InnerText ="x:Margin=0.51181102362204722";//设置文本节点
xe1.AppendChild(xesub1);//添加到<PageSetup>节点中 XmlElement xesub2 = xmlDoc.CreateElement("Footer");
xesub2.InnerText ="x:Margin=0.51181102362204722";//设置文本节点
xe1.AppendChild(xesub2);//添加到<PageSetup>节点中 XmlElement xesub3 = xmlDoc.CreateElement("PageMargins");
xesub3.InnerText ="x:Bottom="0.98425196850393704" x:Left="0.78740157480314965"
x:Right="0.47244094488188981" x:Top="0.98425196850393704";//设置文本节点
xe1.AppendChild(xesub3);//添加到<PageSetup>节点中
.....
root.AppendChild(xe1);
xmlDoc.Save("你的文件名.xml");