将XML文件的元素作为字符串返回

时间:2014-01-09 15:38:36

标签: c# .net xml linq-to-xml

我正在尝试显示用户上传的word文档中的XML内容标记,但我不确定如何显式提取数据,然后将其显示为字符串。

我认为下面应该找到正确的后代(或元素,或者应该正常工作?)然后允许我从中创建一个字符串并显示它,但我无法让文件识别xdoc)。我想要返回的内容位于代码底部的“w.tag”。

有什么想法吗?

WordprocessingDocument _TempDoc = WordprocessingDocument.Open(Server.MapPath("~/") + filename, true);

                            //query to find particular descendants
                            var lv1s = from lv1 in xdoc.Descendants("table")
                                       select new
                                       {
                                           Header = lv1.Attribute("name").Value,
                                           Children = lv1.Descendants("tag")
                                       };

                            //Loop through results
                            StringBuilder result = new StringBuilder();
                            foreach (var lv1 in lv1s)
                                        {
                                            result.AppendLine(lv1.Header);
                                            foreach (var lv2 in lv1.Children)
                                            result.AppendLine("     " + lv2.Attribute("name").Value);
                                        }

                            //the label should contain the content controls of the document, using the class, XMLfromDocument                                                                                                                                           
                            labelContentControls.Text = fileUpload_Displayx(XMLfromDocument.GetContentControls(_TempDoc));

 public static XDocument GetXDocument(this OpenXmlPart part)
        {
            XDocument xdoc = part.Annotation<XDocument>();
            if (xdoc != null)
                return xdoc;
            using (Stream str = part.GetStream())
            using (StreamReader streamReader = new StreamReader(str))
            using (XmlReader xr = XmlReader.Create(streamReader))
                xdoc = XDocument.Load(xr);
            part.AddAnnotation(xdoc);
            return xdoc;

        }    

        //following method gets the structure of the content controls / XML in the document
        public static XElement GetContentControls( WordprocessingDocument document) 

        {

                XElement contentControls = new XElement("ContentControls",
                    document
                        .MainDocumentPart
                        .GetXDocument()
                        .Root
                        .Element(W.body)
                        .Elements(W.sdt)
                        .Select(tableContentControl =>
                            new XElement("Table",
                                new XAttribute("Name", (string)tableContentControl
                                    .Element(W.sdtPr).Element(W.tag).Attribute(
                                        W.val)),
                                tableContentControl
                                        .Descendants(W.sdt)
                                        .Select(fieldContentControl =>
                                                new XElement("Field",
                                                    new XAttribute("Name",
                                                        (string)fieldContentControl
                                                              .Element(W.sdtPr)
                                                              .Element(W.tag)
                                                              .Attribute(W.val)
                                                                   )
                                                            )
                                                )
                                     )
                            )           
            );




         // you cannot access the inner XML of the elemnt  directly, you must concatenate to the child elements.  
         // string contentTitle = string.Concat(W.sdtPr);


        //return W.tag;
                return contentControls;

0 个答案:

没有答案