2016-06-22 29 views
0

我正在寻找一种很好的方法来读取和提取使用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> 

应该看到这样的信息:

定义
组用户在演员订户在请求中将合同名称设置为'请求'中的排除订户Contract-Name;

感谢您的帮助。

回答

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' ; 
+0

感谢您的帮助。 我可以问你几个问题: 是否有避免使用新规则9,因为没有属性xml:space =“preserve”? 该行的用途是什么:nsmgr.AddNamespace(“w”,“urn:http://mynamespace.owner.com”); ? 谢谢。 –

+0

我已经使用xml属性上的过滤器更新了我的答案。我必须指定命名空间'w',因为提供的xml格式不正确:''应该是' '。没有这个声明,解析器会抛出异常,因为名称空间'w'没有为元素'body'声明。 –