Reputation: 1495
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
[email protected]
</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
[email protected]
</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.
Upvotes: 0
Views: 1974
Reputation: 1495
Thank you everyone for your suggestions. With your help I was able to come up with this solution:
// 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();
Upvotes: 0
Reputation: 116108
If your intend is to remove formatting
StringWriter wr = new StringWriter();
XDocument xDoc = XDocument.Load(.....);
xDoc.Save(wr, SaveOptions.DisableFormatting);
var newXml = wr.ToString();
Upvotes: 1
Reputation: 578
I'm a little confused as to what you're asking, but my understanding is this part:
<company>Comp1</company> <company>Comp2</company>
is the new part, and you want it with no indents and with the first company on the same line as the starting "coveragelist" node?
I know you said you tried XmlTextWriter, but have you tried:
xmlTextWriter.Formatting = xmlTextWriter.Formatting.None
and
xmlTextWriter.WriteString("\n");
where you need?
Upvotes: 1