如何使用C#读取这种XML文件?

时间:2016-06-22 08:59:55

标签: c# xml

我正在寻找一种使用C#在xml文件中读取和提取文本的好方法。

我想在-w:t xml:space="preserve- XXXX -/w:t-

中连接文本

以下是文件示例:

  <w:body>
    <w:sdt xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:sdtPr>
        <w:rPr>
          <w:b />
          <w:bCs />
          <w:noProof />
        </w:rPr>
        <w:alias w:val="Business Rule" />
        <w:tag w:val="urn:ILOG.RuleDoc.3:Rule_a9e5674c-7145-4bfb-af53-524543e98358" />
        <w:id w:val="8959571" />
      </w:sdtPr>
      <w:sdtContent>
        <w:p w:rsidR="00711D98" w:rsidRDefault="00711D98" w:rsidP="00711D98">
          <w:pPr>
            <w:pStyle w:val="Rule" />
            <w:rPr>
              <w:b />
              <w:bCs />
              <w:noProof />
            </w:rPr>
          </w:pPr>
        </w:p>
        <w:p w:rsidR="00711D98" w:rsidRPr="00C95F62" w:rsidRDefault="00711D98" w:rsidP="00711D98">
          <w:sdt>
            <w:sdtPr>
              <w:rPr>
                <w:rStyle w:val="Heading3Char" />
                <w:noProof />
              </w:rPr>
              <w:alias w:val="Name" />
              <w:tag w:val="urn:ILOG.RuleDoc.3:RuleProperty" />
              <w:id w:val="7323368" />
              <w:dataBinding w:prefixMappings="xmlns:ns0='http://schemas.ilog.com/Rules/3.0/RuleDocumentData' xmlns:ns1='http://schemas.ilog.com/Rules/1.1/Properties' xmlns:ns2='urn:Intellinsure'" w:xpath="//ns0:ActionRule/ns0:Properties/ns1:Uuid[.='a9e5674c-7145-4bfb-af53-524543e98358']/../ns1:Name" w:storeItemID="{00000000-0000-0000-0000-000000000000}" />
              <w:text />
            </w:sdtPr>
            <w:sdtContent>
              <w:r>
                <w:rPr>
                  <w:rStyle w:val="Heading3Char" />
                </w:rPr>
                <w:t>New Rule 9</w:t>
              </w:r>
            </w:sdtContent>
          </w:sdt>
        </w:p>
        <w:p w:rsidR="00711D98" w:rsidRPr="0032004A" w:rsidRDefault="00711D98" w:rsidP="00711D98">
          <w:pPr>
            <w:pStyle w:val="Rule" />
          </w:pPr>
        </w:p>
        <w:sdt>
          <w:sdtPr>
            <w:alias w:val="Rule Body" />
            <w:tag w:val="urn:ILOG.RuleDoc.3:RuleBody" />
            <w:id w:val="9275215" />
          </w:sdtPr>
          <w:sdtContent xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
            <w:p>
              <w:pPr>
                <w:pStyle w:val="RuleBody" />
              </w:pPr>
              <w:r>
                <w:rPr>
                  <w:rStyle w:val="RuleBodyNormal" />
                </w:rPr>
                <w:t xml:space="preserve">definitions</w:t>
              </w:r>
            </w:p>
            <w:p>
              <w:pPr>
                <w:pStyle w:val="RuleBody" />
              </w:pPr>
              <w:r>
                <w:tab />
              </w:r>
              <w:r>
                <w:rPr>
                  <w:rStyle w:val="RuleBodyNormal" />
                </w:rPr>
                <w:t xml:space="preserve">set 'subscriber' to an actor in the subscribers to "Contract-Name" in 'the request' ;</w:t>
              </w:r>
            </w:p>
            <w:p>
              <w:pPr>
                <w:pStyle w:val="RuleBody" />
              </w:pPr>
              <w:r>
                <w:tab />
              </w:r>
              <w:r>
                <w:rPr>
                  <w:rStyle w:val="RuleBodyNormal" />
                </w:rPr>
                <w:t xml:space="preserve">set 'excludedSubscribersList' to the excluded subscribers to "Contract-Name" in 'the request' ;</w:t>
              </w:r>
            </w:p>

应该给出类似的东西:

解释
将订阅者设置为订阅者中的参与者以在请求中签订合同名称 将“excludedSubscribersList设置为”请求“中Contract-Name的被排除订阅者;

谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

使用XmlReader

StringBuilder content = new StringBuilder();
// open the file
using (Stream file = File.OpenRead("test.xml"))
{
    XmlReaderSettings settings = new XmlReaderSettings();
    //must use this to declare the 'w' namespace
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
    nsmgr.AddNamespace("w", "urn:http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    XmlParserContext ctx = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

    using (XmlReader reader = XmlReader.Create(file, settings, ctx))
    {
        while (reader.Read())
        {
            // parse content of 't' elments and add them to the string builder
            if (reader.NodeType == XmlNodeType.Element && reader.LocalName.Equals("t"))
            {
                if ("preserve".Equals(reader.GetAttribute("xml:space")))
                {
                    reader.ReadStartElement();
                    content.Append(reader.ReadContentAsString());
                    content.AppendLine();
                }

            }
        }
    }
}

给出了:

definitions
set 'subscriber' to an actor in the subscribers to "Contract-Name" in 'the request' ;
set 'excludedSubscribersList' to the excluded subscribers to "Contract-Name" in 'the request' ;
相关问题