【问题标题】:XML parsing using C# for sibling element with namespace使用 C# 对具有命名空间的兄弟元素进行 XML 解析
【发布时间】:2015-04-01 12:28:24
【问题描述】:

我有一个复杂的 XML,想在 C# 中使用 LINQ 解析它:

<?xml version="1.0"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xml:space="preserve" w:embeddedObjPresent="no">
<w:docPr xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0">
    <w:displayBackgroundShape/>
    <w:view w:val="print"/>
    <w:zoom w:percent=""/>
    <w:defaultTabStop w:val="708.1365"/>
    <w:docVars/>
</w:docPr>
<w:body>
    <w:p xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0">
    <w:pPr>
    <w:pStyle w:val="Author_28_s_29_"/>
    </w:pPr>
    <w:r>
    <w:rPr><w:rStyle w:val="T4"/></w:rPr>
    <w:t>Satyam</w:t>
    </w:r>
    <w:r>
    <w:rPr>
    <w:rStyle w:val="T5"/>
    </w:rPr>
    <w:t>Singh</w:t>
    </w:r>
    <w:r>
    <w:t>,</w:t>
    </w:r>
    <w:r>
    <w:rPr>
    <w:rStyle w:val="T6"/>
    </w:rPr>
    <w:t>Disha</w:t>
    </w:r>
    <w:r>
    <w:t>A</w:t>
    </w:r>
    <w:r>
    <w:rPr>
    <w:rStyle w:val="T4"/>
    </w:rPr>
    <w:t>.</w:t>
    </w:r>
    <w:r>
    <w:rPr>
    <w:rStyle w:val="T5"/>
    </w:rPr>
    <w:t>Shah</w:t>
    </w:r>
    <w:r>
    <w:rPr>
    <w:rStyle w:val="T7"/>
    </w:rPr>
    <w:t>,2,*</w:t>
    </w:r>
    <w:r>
    <w:t>,</w:t>
    </w:r>
    <w:r>
    <w:rPr>
    <w:rStyle w:val="T4"/>
    </w:rPr>
    <w:t>Karan</w:t>
    </w:r>
    <w:r>
    <w:rPr>
    <w:rStyle w:val="T5"/>
    </w:rPr>
    <w:t>Bhutwala</w:t>
    </w:r>
   </w:p>
</w:body>
</w:wordDocument>

所以我想得到:

Author_28_s_29_=萨蒂扬·辛格

Author_28_s_29_=迪沙阿沙

Author_28_s_29_=卡兰·布特瓦拉

.

.

等等。

我尝试了几个解析选项。这里的逻辑可以理解为

parent().children.where(r).(t).value

【问题讨论】:

  • 请展示一个简短但完整的程序,说明您尝试过的内容,并说明您的预期与发生的情况。 (如果您将 XML 格式化以使结构清晰可见,这也真的会有所帮助。)

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


【解决方案1】:

这个xml很奇怪。也许有更简单的方法,但我只有这个:

 XDocument xd = XDocument.Load("1.xml");
XNamespace nms = "http://schemas.microsoft.com/office/word/2003/wordml";

    var author = xd.Root.Element(nms + "body")
                .Descendants(nms + "pStyle")
                .Single()
                .Attribute(nms + "val").Value;

    var arr = string.Join(" ", xd.Root.Element(nms + "body")
                .Descendants(nms + "t").Select(y => y.Value))
                .Split(',').Where(y => y.Length > 2)
                .Select(y => string.Format("{0}={1}", author, y.Trim()))
                .ToArray();

    foreach (var x in arr)
        Console.WriteLine(x);

输出:

Author_28_s_29_=Satyam Singh
Author_28_s_29_=Disha A . Shah
Author_28_s_29_=Karan Bhutwala

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    相关资源
    最近更新 更多