使用XMLWRITER 可以写出一个元素
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(m_Document);
writer.WriteElementString("langstring", "xmltest");<langstring> xmltest </langstring>
如果要写成下面这个样子应该用代码如何书写呢?
<langstring xml:lang="zh">xmltest</langstring>
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(m_Document);
writer.WriteElementString("langstring", "xmltest");<langstring> xmltest </langstring>
如果要写成下面这个样子应该用代码如何书写呢?
<langstring xml:lang="zh">xmltest</langstring>
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create ("c:\\1.xml");
writer.WriteComment("sample XML fragment");
// Write an element (this one is the root).
writer.WriteStartElement("book");
// Write the namespace declaration.
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
// Write the genre attribute.
writer.WriteAttributeString("genre", "novel");
// Write the title.
writer.WriteStartElement("title");
writer.WriteString("The Handmaid's Tale");
writer.WriteEndElement();
// Write the price.
writer.WriteElementString("price", "19.95");
// Lookup the prefix and write the ISBN element.
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartElement(prefix, "ISBN", "urn:samples");
writer.WriteString("1-861003-78");
writer.WriteEndElement(); // Write the style element (shows a different way to handle prefixes).
writer.WriteElementString("style", "urn:samples", "hardcover");
// Write the close tag for the root element.
writer.WriteEndElement();
// Write the XML to file and close the writer.
writer.Flush();
writer.Close();
} finally {
if (writer != null)
writer.Close();
}
问题是上面这样的写法我实现不出来:(
writer.WriteAttributeString("language", "zh");
writer.WriteValue("xmltest");
writer.WriteEndElement();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(file, settings))
{
writer.WriteStartElement("langstring");
writer.WriteAttributeString("xml", "lang", null, "zh");
writer.WriteString("xmltest");
writer.WriteEndElement();
writer.Flush();
writer.Close();
}