用C#中的文本分隔XML文件的部分

时间:2017-06-19 16:01:13

标签: c# xml

我需要编写带有波浪号的XML文件作为部分之间的分隔符,如下所示:

....
<Company>
<CompanyDetail>Blah</CompanyDetail>
<Phone>0000000000</Phone>
</Company>
~
<Company>
<CompanyDetail>Blah</CompanyDetail>
<Phone>0000000000</Phone>
</Company>
....

通常在C#中这样做的方法将是

writer = new XmlTextWriter(fileName, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();

int remainingCompanies = companyList.Count;

foreach (Company company in companyList)
{
    writer.WriteStartElement("Company");
    writer.WriteStartElement("CompanyDetail");
    writer.WriteString("company.companyDetail.toString()");
    writer.WriteEndElement();
    writer.WriteStartElement("Phone");
    writer.WriteString("company.phone.toString()");
    writer.WriteEndElement();
    writer.WriteEndElement();

    if (remainingCompanies-- > 1)
    {
        writer.WriteString(\n~\n);
    }
}

但是每当我这样做时,生成的XML文件的格式都很差,如下所示:

<Company>
<CompanyDetail>FirstCompany</CompanyDetail>
<Phone>1111111111</Phone>
</Company>
~
<Company><CompanyDetail>SecondCompany</CompanyDetail><Phone>2222222222</Phone></Company>
~
<Company><CompanyDetail>ThirdCompany</CompanyDetail><Phone>3333333333</Phone></Company>
....

当每家公司提供的信息远远超过CompanyDetail和Phone时,您可以想象通过单行查看XML所需的内容是多么困难。

我目前的解决方法是用注释替换波浪号,但是如何使用波形符分隔此XML文件的部分并保持干净的格式?

0 个答案:

没有答案