c# - Write to existing xml file without replacing it's contents (XmlWriter) -
i encountered following issue,
i first write xml file this:
xmltextwriter writer = new xmltextwriter("course.xml", null); writer.formatting = formatting.indented; writer.writestartdocument(); writer.writestartelement("course"); writer.writeattributestring("title", "examle"); writer.writeattributestring("started", "true"); writer.writeendelement(); writer.writeenddocument(); writer.close();
and xml output is:
<?xml version="1.0"?> <course title="example" started="true" />
after want write more data xml file use code again:
xmltextwriter writer = new xmltextwriter("course.xml", null); writer.formatting = formatting.indented; writer.writestartdocument(); writer.writestartelement("course"); writer.startelement("level"); writer.startelement("module"); writer.endelement(); writer.endelement(); writer.writeendelement(); writer.writeenddocument(); writer.close();
and xml output is:
<?xml version="1.0"?> <course> <level> <module> </module> </level> </course>
so replaces original data, , attributes in course tag. therefore need way doesn't replace data, instead add inside existing tags.
xml files sequential text files. not database or random-access file. there no way write middle of them.
Comments
Post a Comment