我用xDocument.Root.Element("w:node")会报不能有“:”冒号的错误,
改成xDocument.Root.Element("node")也报错,
w是命名空间的定义,
我想知道如何用System.Xml.Linq.XDocument(不是XmlDocument)读取带有命名空间的XML的节点,
最好给个例子,第一次写读取XML的代码,还请高手指教,谢谢也顺便给个XmlDocument读取带有命名空间的XML的节点的例子吧

解决方案 »

  1.   

    http://msdn.microsoft.com/zh-tw/library/bb387075.aspx
      

  2.   

    private int WriteDocxTable()
            {
                if (_docxPackage.PartExists(_uriDocument))
                {
                    _xDocument = GetXmlFromUri(_uriDocument);
                    XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
                    IEnumerable<XElement> tables = _xDocument.Root.Element(w + "body").Elements(w + "tbl");
                    if (Enumerable.Count(tables) > 0)
                    {
                        _xElement = Enumerable.ElementAt(tables, Enumerable.Count(tables) - 1);
                        // 取得rsidR,修改标识符,可以没有
                        string rsidR = _xElement.Element(w + "tr").Attribute(w + "rsidR").Value;
                        _xElement.Add(new XElement(w + "tr",
                            //new XAttribute(w + "rsidR", rsidR),
                            //new XAttribute(w + "rsidTr", rsidR),
                            new XElement(w + "tc",
                                new XElement(w + "tcPr",
                                    new XElement(w + "tcW",
                                        new XAttribute(w + "w", "2901"),
                                        new XAttribute(w + "type", "dxa"))),
                                new XElement(w + "p",
                                    //new XAttribute(w + "rsidR", rsidR),
                                    new XAttribute(w + "rsidRDefault", rsidR)))));
                    }
                    PackagePart _partDocument = _docxPackage.GetPart(_uriDocument);
                    // 保存到文件
                    SavePart(_partDocument, _xDocument);
                    //SavePart(_docxPackage, _xDocument);            }
                return 0;
            }