2016-06-22 47 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'聲明。 –