??The code shown in Figure 12 demonstrates the advantage of the client using the XmlTextReadWriter class to modify attribute values while reading. The C# and VB source code download of the XmlTextReadWriter class is provided in this issue of msdn (see the link provided at the beginning of this article).
Figure 12 Changing Attribute Values
private void ApplyChanges(string nodeName, string attribName,
string oldVal, string newVal)
{
XmlTextReadWriter rw = new XmlTextReadWriter(InputFileName.Text,
OutputFileName. Text);
rw.WriteStartDocument(true, CommentText.Text);
// Manually modify the root node
rw.Writer.WriteStartElement(rw.Reader.LocalName);
// Start modifying attributes
// ( You can modify the attributes of more nodes)
rw.AddAttributeChange(nodeName, attribName, oldVal, newVal);
// Loop processing documents
while(rw.Read())
{
switch(rw .NodeType)
{
case .Writeattributes (nodename);
Else
// Deep Copy
rw.writer.writeattributes (RW.Reader, FALSE); IteEndelement ( );
break;
}
}
// Close the root tag
rw.Writer.WriteEndElement(); ment ();
}
??The XmlTextReadWriter class can not only read XML documents, but also write XML documents. You can use it to read the contents of XML documents, and if necessary, you can also use it to do some basic update operations. Basic update operation here refers to modifying the value of an existing attribute or the content of a node, or adding a new attribute or node. For more complex operations, it is best to use an XMLDOM parser.
??Summary
??Reader and Writer are the basics for processing XML data in the .NET Framework. They provide the original API for all XML data access functions. Reader is like a new parser class that has the power of XMLDOM and the speed and simplicity of SAX. Writer is designed for simple creation of XML documents. Although Reader and Writer are both small pieces of the .NET Framework, they are independent APIs. In this article, we only discussed how to use Reader and Writer to complete some important work, introduced the principle mechanism of the verification analyzer, and integrated Reader and Writer in a separate class. All of the above classes are lightweight, similar to cursor-based XMLDOM parsers.
The above is the content of easily processing XML data (5-2) in .NET Framework. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!