今天我看到一个例子:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/2/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:SL="http://schemas.microsoft.com/schemaLibrary/2003/2/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/2/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xml:space="preserve">
<v:body>
<o:t> 过去常喝茶,但现在不喝茶了。</o:t>
</v:body>
</w:wordDocument>这里面的元素<wordDocument>有这很很多多的命名空间,请问这是怎么加进去的?请给个简单代
码还有如果要查找w:wordDocument/v:body/o:t元素的内容该怎么办?请给我简单代码谢谢大家
答完就揭贴!(每问50分,我知道这不是菜市场,我说的是揭贴的标准给分)

解决方案 »

  1.   

    XmlWriter提供了生成名称空间的的方法,以下是生成Data2.xml格式的例子。
    C#:
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Xml" %>
    <script runat="server">
      protected void Page_Load(object sender, EventArgs e)
      {
        XmlWriterSettings xws = new XmlWriterSettings();
        xws.CheckCharacters = true;
        xws.CloseOutput = true;
        xws.ConformanceLevel = ConformanceLevel.Document;
        xws.Encoding = System.Text.Encoding.UTF8;
        xws.Indent = true;
        xws.NewLineOnAttributes = true;
        XmlWriter xw = XmlWriter.Create(Server.MapPath("Data2_1.xml"), xws);
        xw.WriteStartDocument();
        xw.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
        xw.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
        xw.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
        xw.WriteStartElement(xw.LookupPrefix("http://schemas.xmlsoap.org/soap/envelope/"), "Body", 
          "http://schemas.xmlsoap.org/soap/envelope/");
        xw.WriteStartElement("GetUserInfo", "http://dotnet.aspx.cc/");
        xw.WriteElementString("cert", "string");
        xw.WriteElementString("guid", "string");
        xw.WriteFullEndElement();
        xw.Close();
      }
    </script>
    VB.NET:
    <%@ Page Language="VB" Debug="true" %>
    <%@ Import Namespace="System.Xml" %>
    <script runat="server">
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim xws As XmlWriterSettings = New XmlWriterSettings
        xws.CheckCharacters = True
        xws.CloseOutput = True
        xws.ConformanceLevel = ConformanceLevel.Document
        xws.Encoding = System.Text.Encoding.UTF8
        xws.Indent = True
        xws.NewLineOnAttributes = True
        Dim xw As XmlWriter = XmlWriter.Create(Server.MapPath("Data2_1.xml"), xws)
        xw.WriteStartDocument()
        xw.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
        xw.WriteAttributeString("xmlns", "xsi", Nothing, "http://www.w3.org/2001/XMLSchema-instance")
        xw.WriteAttributeString("xmlns", "xsd", Nothing, "http://www.w3.org/2001/XMLSchema")
        xw.WriteStartElement(xw.LookupPrefix("http://schemas.xmlsoap.org/soap/envelope/"), "Body", _
                            "http://schemas.xmlsoap.org/soap/envelope/")
        xw.WriteStartElement("GetUserInfo", "http://dotnet.aspx.cc/")
        xw.WriteElementString("cert", "string")
        xw.WriteElementString("guid", "string")
        xw.WriteFullEndElement()
        xw.Close()
      End Sub
    </script>
      

  2.   

    XmlDocument方法创建System.Xml.XmlDocument doc = new System.Xml.XmlDocument();System.Xml.XmlNode root = doc.CreateNode(System.Xml.XmlNodeType.Element, "w", "wordDocument", "http://schemas.microsoft.com/office/word/2003/2/wordml");
    System.Xml.XmlAttribute xa;
    xa = doc.CreateAttribute("xmlns", "v", "http://www.w3.org/2000/xmlns/");
    xa.Value = "urn:schemas-microsoft-com:vml";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "w10", "http://www.w3.org/2000/xmlns/");
    xa.Value = "urn:schemas-microsoft-com:office:word";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "SL", "http://www.w3.org/2000/xmlns/");
    xa.Value = "http://schemas.microsoft.com/schemaLibrary/2003/2/core";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "aml", "http://www.w3.org/2000/xmlns/");
    xa.Value = "http://schemas.microsoft.com/aml/2001/core";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "wx", "http://www.w3.org/2000/xmlns/");
    xa.Value = "http://schemas.microsoft.com/office/word/2003/2/auxHint";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "o", "http://www.w3.org/2000/xmlns/");
    xa.Value = "urn:schemas-microsoft-com:office:office";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "dt", "http://www.w3.org/2000/xmlns/");
    xa.Value = "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "space", "http://www.w3.org/2000/xmlns/");
    xa.Value = "preserve";
    root.Attributes.Append(xa);System.Xml.XmlNode body = doc.CreateNode(System.Xml.XmlNodeType.Element, "v", "body", "urn:schemas-microsoft-com:vml");System.Xml.XmlNode childNode = doc.CreateNode(System.Xml.XmlNodeType.Element, "o", "t", "urn:schemas-microsoft-com:office:office");
    childNode.InnerText = "过去常喝茶,但现在不喝茶了。";
    body.AppendChild(childNode);
    root.AppendChild(body);
    doc.AppendChild(root);
    doc.Save(@"c:\test.xml");
      

  3.   

    非常感谢net_lover(【孟子E章】) !!!请问一下怎么实现:
    还有如果要查找w:wordDocument/v:body/o:t元素的内容该怎么办?
      

  4.   

    nsmanager.AddNamespace("w","http://schemas.microsoft.com/office/word/2003/2/wordml");   
    nsmanager.AddNamespace("v","urn:schemas-microsoft-com:vml"); 
    nsmanager.AddNamespace("o","urn:schemas-microsoft-com:office:office"); 
    XmlNode node = myXmlDocument.SelectSingleNode("w:wordDocument/v:body/o:t",nsmanager);上面的代码可以实现吗?(我是不可以的)
      

  5.   

    <%@ Import Namespace="System.Xml" %>   ok 其它的就不明白了。net_lover(【孟子E章】) 都有6年了吧
      

  6.   

    是可以的阿
    直接拷贝即可运行System.Xml.XmlDocument doc = new System.Xml.XmlDocument();System.Xml.XmlNode root = doc.CreateNode(System.Xml.XmlNodeType.Element, "w", "wordDocument", "http://schemas.microsoft.com/office/word/2003/2/wordml");
    System.Xml.XmlAttribute xa;
    xa = doc.CreateAttribute("xmlns", "v", "http://www.w3.org/2000/xmlns/");
    xa.Value = "urn:schemas-microsoft-com:vml";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "w10", "http://www.w3.org/2000/xmlns/");
    xa.Value = "urn:schemas-microsoft-com:office:word";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "SL", "http://www.w3.org/2000/xmlns/");
    xa.Value = "http://schemas.microsoft.com/schemaLibrary/2003/2/core";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "aml", "http://www.w3.org/2000/xmlns/");
    xa.Value = "http://schemas.microsoft.com/aml/2001/core";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "wx", "http://www.w3.org/2000/xmlns/");
    xa.Value = "http://schemas.microsoft.com/office/word/2003/2/auxHint";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "o", "http://www.w3.org/2000/xmlns/");
    xa.Value = "urn:schemas-microsoft-com:office:office";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "dt", "http://www.w3.org/2000/xmlns/");
    xa.Value = "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "space", "http://www.w3.org/2000/xmlns/");
    xa.Value = "preserve";
    root.Attributes.Append(xa);System.Xml.XmlNode body = doc.CreateNode(System.Xml.XmlNodeType.Element, "v", "body", "urn:schemas-microsoft-com:vml");System.Xml.XmlNode childNode = doc.CreateNode(System.Xml.XmlNodeType.Element, "o", "t", "urn:schemas-microsoft-com:office:office");
    childNode.InnerText = "过去常喝茶,但现在不喝茶了。";
    body.AppendChild(childNode);
    root.AppendChild(body);
    doc.AppendChild(root);
    //doc.Save(Server.MapPath("test.xml"));System.Xml.XmlNamespaceManager nsmanager = new System.Xml.XmlNamespaceManager(doc.NameTable);
    nsmanager.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/2/wordml");
    nsmanager.AddNamespace("v", "urn:schemas-microsoft-com:vml");
    nsmanager.AddNamespace("o", "urn:schemas-microsoft-com:office:office");    
    System.Xml.XmlNode node = doc.SelectSingleNode("w:wordDocument/v:body/o:t", nsmanager);
    Response.Write(node.InnerText);
      

  7.   

    加上xml声明,完整的代码如下string w3NameSpace = "http://www.w3.org/2000/xmlns/";
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();System.Xml.XmlNode root = doc.CreateNode(System.Xml.XmlNodeType.Element, "w", "wordDocument", "http://schemas.microsoft.com/office/word/2003/2/wordml");
    System.Xml.XmlAttribute xa;
    xa = doc.CreateAttribute("xmlns", "v", w3NameSpace);
    xa.Value = "urn:schemas-microsoft-com:vml";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "w10", w3NameSpace);
    xa.Value = "urn:schemas-microsoft-com:office:word";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "SL", w3NameSpace);
    xa.Value = "http://schemas.microsoft.com/schemaLibrary/2003/2/core";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "aml", w3NameSpace);
    xa.Value = "http://schemas.microsoft.com/aml/2001/core";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "wx", w3NameSpace);
    xa.Value = "http://schemas.microsoft.com/office/word/2003/2/auxHint";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "o", w3NameSpace);
    xa.Value = "urn:schemas-microsoft-com:office:office";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "dt", w3NameSpace);
    xa.Value = "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882";
    root.Attributes.Append(xa);xa = doc.CreateAttribute("xmlns", "space", w3NameSpace);
    xa.Value = "preserve";
    root.Attributes.Append(xa);System.Xml.XmlNode body = doc.CreateNode(System.Xml.XmlNodeType.Element, "v", "body", "urn:schemas-microsoft-com:vml");System.Xml.XmlNode childNode = doc.CreateNode(System.Xml.XmlNodeType.Element, "o", "t", "urn:schemas-microsoft-com:office:office");
    childNode.InnerText = "欢迎光临【孟宪会之精彩世界】";
    body.AppendChild(childNode);
    root.AppendChild(body);
    doc.AppendChild(root);System.Xml.XmlDeclaration xd = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
    doc.InsertBefore(xd,doc.DocumentElement);System.Xml.XmlProcessingInstruction spi = doc.CreateProcessingInstruction("mso-application","progid=\"Word.Document\"");
    doc.InsertBefore(spi, doc.DocumentElement);
    doc.Save(Server.MapPath("test.xml"));System.Xml.XmlNamespaceManager nsmanager = new System.Xml.XmlNamespaceManager(doc.NameTable);
    nsmanager.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/2/wordml");
    nsmanager.AddNamespace("v", "urn:schemas-microsoft-com:vml");
    nsmanager.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
    System.Xml.XmlNode node = doc.SelectSingleNode("w:wordDocument/v:body/o:t", nsmanager);
    Response.Write(node.InnerText);