使用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>

解决方案 »

  1.   

    XmlWriter writer = null;      try {
         
            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();
         }
      

  2.   

    msdn我看过了,就是因为没找到方法才到这提问的
      

  3.   

    <langstring xml:lang="zh">xmltest</langstring>
    问题是上面这样的写法我实现不出来:(
      

  4.   

    writer.WriteStartElement("string");
                writer.WriteAttributeString("language", "zh");
                writer.WriteValue("xmltest");
                writer.WriteEndElement();
      

  5.   

    string file = "e:\\1.xml";
    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();
    }