0

I want to modify a XML file but the tricky part is that the info I add should have minimal formatting. I have been searching for a while and I can't seem to find an answer. Here's what the XML looks currently (original):

<?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>

It has to look like this:

<?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>

Here's my code so far:

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);

I've tried XMLTextWriter but I didn't have any luck. I really appreciate any help.

Thank you in advance.

4

3 回答 3

1

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

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

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

我知道您说过您尝试过 XmlTextWriter,但您是否尝试过:

xmlTextWriter.Formatting = xmlTextWriter.Formatting.None

xmlTextWriter.WriteString("\n");

你需要在哪里?

于 2012-05-09T20:33:36.857 回答
1

如果您打算删除格式

StringWriter wr = new StringWriter();
XDocument xDoc = XDocument.Load(.....);
xDoc.Save(wr, SaveOptions.DisableFormatting);
var newXml = wr.ToString();
于 2012-05-09T20:34:12.683 回答
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();
于 2012-05-28T14:46:43.803 回答