質問

I have an xml file likes below.

<?xml version="1.0" encoding="utf-8" ?>
<Book>
  <Title>Title</Title>
  <Content>Content</Content>
</Book>

I want to write a new node after 'Content', I know how to use XMLDocument to do that, is there a way to use XMLTextWriter to do that?

役に立ちましたか?

解決

You will have to write the whole Xml document, i.e. all elements and attributes and attribute values by using the XmlTextWriter. After you've written the <Content> element, you can write your additional element.

Something like this:

writer.WriteStartDocument();
writer.WriteStartElement("Book");
writer.WriteStartElement("Title");
writer.WriteString("Title");
writer.WriteEndElement();
writer.WriteStartElement("Content");
writer.WriteString("Content");
writer.WriteEndElement();
// insert your new data here
writer.WriteEndElement();
writer.WriteEndDocument();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top