删除XML文件中的特殊情况缩进

时间:2012-05-09 20:19:33

标签: c# xml c#-4.0 xmldocument xmltextwriter

我想修改一个XML文件,但棘手的部分是我添加的信息应该具有最小的格式。我一直在寻找,我似乎无法找到答案。以下是XML当前的内容(原始):

<?xml version="1.0" encoding="utf-8"?>
<doe-jane>
  <name>Jane Doe</name>
  <contact>North Pole

Tel: (555) 123-4567
jane.doe@abc.com
</contact>
  <coveragelist>
    <company>Comp1</company>
    <company>Comp2</company>
  </coveragelist>
</doe-jane>

它必须如下所示:

<?xml version="1.0" encoding="utf-8"?>
<doe-jane>
  <name>Jane Doe</name>
  <contact>North Pole

Tel: (555) 123-4567
jane.doe@abc.com
</contact>
--> // Change needs to happen from here on <--
<coveragelist><company>Comp1</company>
<company>Comp2</company>
</coveragelist>
</doe-jane>

到目前为止,这是我的代码:

XmlDocument d = new XmlDocument();
//XmlTextWriter wr = new XmlTextWriter(resAnXFile, Encoding.UTF8);
//wr.Formatting = Formatting.None;
d.Load(resAnXFile);
XmlNode t = d.SelectSingleNode("//coveragelist");
t.ParentNode.RemoveChild(t);
// create CoverageList node
XmlNode coverageListNode = d.CreateNode(XmlNodeType.Element, "coveragelist", null);
foreach (var company in dataList)
{
    // create company nodes
    XmlNode companyNode = d.CreateElement("company");
    companyNode.InnerText = company.CompanyName.ToString();
    coverageListNode.AppendChild(companyNode);
}
d.DocumentElement.AppendChild(coverageListNode);
d.Save(resAnXFile);

我已经尝试过XMLTextWriter,但我没有运气。我真的很感激任何帮助。

提前谢谢。

3 个答案:

答案 0 :(得分:1)

我对你的要求感到有点困惑,但我的理解是这一部分:

    <company>Comp1</company>
    <company>Comp2</company>

是新部件,您希望它没有缩进,并且第一家公司与起始“coveragelist”节点位于同一行?

我知道你说过你尝试过XmlTextWriter,但是你试过了吗?

xmlTextWriter.Formatting = xmlTextWriter.Formatting.None

xmlTextWriter.WriteString("\n");

你需要的地方?

答案 1 :(得分:1)

如果您打算删除格式

StringWriter wr = new StringWriter();
XDocument xDoc = XDocument.Load(.....);
xDoc.Save(wr, SaveOptions.DisableFormatting);
var newXml = wr.ToString();

答案 2 :(得分:0)

谢谢大家的建议。在您的帮助下,我能够提出这个解决方案:

// Open up the same file and remove xml auto-formatting
XmlReader reader = XmlReader.Create(readFileName);
XmlTextWriter writer = new XmlTextWriter(writeFileName, null);
while (reader.Read())
{
switch (reader.NodeType)
{
    case XmlNodeType.Element:
        // if 1st node after openening tag is analyst name then setup a linefeed
        if (reader.Name.Equals(Path.GetFileNameWithoutExtension(readerFileName)))
        {
            writer.WriteStartElement(reader.Name);
            writer.WriteString("\r\n");
        }
        else
        {
            // setup linefeed after every element
            writer.WriteStartElement(reader.Name);
            writer.WriteAttributes(reader, true);
            if (reader.IsEmptyElement)
            {
                writer.WriteEndElement();
                writer.WriteString("\r\n");
            }
        }
        break;

    case XmlNodeType.Text:
        writer.WriteString(reader.Value);
        break;

    case XmlNodeType.EndElement:
        writer.WriteEndElement();
        break;

    // handles opening xml tag
    case XmlNodeType.XmlDeclaration:
    case XmlNodeType.ProcessingInstruction:
        writer.WriteProcessingInstruction(reader.Name, reader.Value);
        writer.WriteString("\r\n");
        break;
}
}
// close reader & writer
writer.Flush();
reader.Close();