2012-11-07 37 views
10

जोड़ना मेरे पास एमएस शब्द दस्तावेज़ .docx के रूप में सहेजा गया है। मैं docx की XML फ़ाइल को संपादित करके अपने पाठ में नई पंक्ति डालना चाहता हूं। मैंने पहले ही 
, 
, 
, 	 की कोशिश की है, एमडी यह हमेशा मुझे केवल एक नई लाइन नहीं देता है।एक्सएमएल - नई लाइन

यह क्या करता है:

(XML कोड) <w:t>hel&#xA;lo</w:t>

जब मैं .docx फ़ाइल तो खोल यह बदल गया है करने के लिए:

Hel lo नहीं के रूप में मैं एक पंक्ति और lo पर Hel बनना चाहता था सेकेंड लाइन पर।

+0

आप शब्द में संपादित कर की कोशिश की है, और मतभेदों की जांच? –

+0

हां मैंने यह जैसा कुछ किया है ... लेकिन मुझे नए लाइन चरित्र के लिए कोड का उपयोग करने की आवश्यकता है, क्योंकि मैं डीबी से डेटा लोड कर रहा हूं और मैं जो भी नाम लोड करूंगा, वह प्रत्येक को नई लाइन पर रखना चाहता है ... मुझे आशा है कि आप समझेंगे मेरा मतलब क्या है –

+0

क्या आप वास्तव में .docx फ़ाइलों को संपादित कर रहे हैं? कैसे? (वे एक्सएमएल नहीं हैं, लेकिन ज़िप्ड एक्सएमएल हैं।) –

उत्तर

26

<w:br/> टैग का उपयोग करें।

मैंने इसे Word दस्तावेज़ बनाकर पाया, इसे एक्सएमएल (सेव एज़ के माध्यम से) के रूप में सहेज लिया, Shift Enter के साथ एक मजबूर लाइन ब्रेक जोड़कर, परिवर्तन को चेक आउट किया। आवश्यक अंतर केवल w:br टैग प्रतीत होता है, जो स्पष्ट रूप से HTML br टैग को प्रतिबिंबित करता है।

+0

आपने मुझे बहुत समय बचाया! जवाब के लिए Thx! –

+0

स्पष्ट प्रतीत हो सकता है लेकिन वास्तव में क्या करने की आवश्यकता है ' 'टैग को सभी को ' के साथ बदलना है ... – Sebas

2

मामले में यह किसी को भी मदद करता है, सी # कोड की निम्न बिट बहु लाइन XML संरचना

//Sets the text for a Word XML <w:t> node 
//If the text is multi-line, it replaces the single <w:t> node for multiple nodes 
//Resulting in multiple Word XML lines 
private static void SetWordXmlNodeText(XmlDocument xmlDocument, XmlNode node, string newText) 
{ 

    //Is the text a single line or multiple lines?> 
    if (newText.Contains(System.Environment.NewLine)) 
    { 
     //The new text is a multi-line string, split it to individual lines 
     var lines = newText.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 


     //And add XML nodes for each line so that Word XML will accept the new lines 
     var xmlBuilder = new StringBuilder(); 
     for (int count = 0; count < lines.Length; count++) 
     { 
      //Ensure the "w" prefix is set correctly, otherwise docFrag.InnerXml will fail with exception 
      xmlBuilder.Append("<w:t xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\">"); 
      xmlBuilder.Append(lines[count]); 
      xmlBuilder.Append("</w:t>"); 

      //Not the last line? add line break 
      if (count != lines.Length - 1) 
      { 
       xmlBuilder.Append("<w:br xmlns:w=\"http://schemas.microsoft.com/office/word/2003/wordml\" />"); 
      } 
     } 

     //Create the XML fragment with the new multiline structure 
     var docFrag = xmlDocument.CreateDocumentFragment(); 
     docFrag.InnerXml = xmlBuilder.ToString(); 
     node.ParentNode.AppendChild(docFrag); 

     //Remove the single line child node that was originally holding the single line text, only required if there was a node there to start with 
     node.ParentNode.RemoveChild(node); 
    } 
    else 
    { 
     //Text is not multi-line, let the existing node have the text 
     node.InnerText = newText; 
    } 
} 

कोड ऊपर आवश्यक बच्चे नोड्स और कैरिएज रिटर्न पैदा करेगा पैदा करेगा, और उपसर्ग का ख्याल रखता है भी।

0

आधार पर @ ऊपर लेनी के जवाब, यह क्या किसी Mac एमएस वर्ड 2011 से मेरी स्थिति में Obj सी का उपयोग कर काम करता है:

- (NSString *)setWordXMLText:(NSString *)str 
{ 
    NSString *newStr = @""; 
    // split the string into individual lines 
    NSArray *lines = [str componentsSeparatedByString: @"\n"]; 

    if (lines.count > 1) 
    { 
     // add XML nodes for each line so that Word XML will accept the new lines 
     for (int count = 0; count < lines.count; count++) 
     { 
      newStr = [newStr stringByAppendingFormat:@"<w:t>%@</w:t>", lines[count]]; 

      // Not the last line? add a line break 
      if (count != lines.count - 1) 
      { 
       newStr = [newStr stringByAppendingString:@"<w:br/>"]; 
      } 
     } 

     return newStr; 
    } 
    else 
    { 
     return str; 
    } 
} 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^