[英]Format and rewrite xml file
I have this xml file. 我有这个xml文件。
<?xml version="1.0" encoding="utf-8"?><test1 name="all">
<section name="Header"><holder name="top" >top</holder><holder name="banner">banner</holder></section>
</test1>
I want to format this file in a manage way like this(at least line break): 我想以这种管理方式格式化文件(至少换行):
<?xml version="1.0" encoding="utf-8"?>
<test1 name="all">
<section name="Header">
<holder name="top" >top</holder>
<holder name="banner">banner</holder>
</section>
</test1>
in c# 在C#中
XDocument xmlDoc = XDocument.Load("file.xml"); //load file
foreach(xml node ...) //Need help for this logic
{
//...Add line break ....
}
If you want to get file with formatted XML, then you just save xmlDoc
. 如果要获取格式为XML的文件,则只需保存
xmlDoc
。
XDocument xmlDoc = XDocument.Load("file.xml"); //load file
xmlDoc.Save("formatted.file.xml");
But it will be fully formatted (with indentations). 但是它将完全格式化(带有缩进)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.