2012-11-07 12 views
10

Ho MS Word doc salvata come .docx. Voglio inserire una nuova riga nel mio testo modificando il file XML di docx. Ho già provato 
, 
, 
, 	, e mi dà sempre solo spazio non una nuova riga.XML - aggiunta nuova riga

ciò che fa:

(codice XML) <w:t>hel&#xA;lo</w:t>

Quando apro .docx lima allora è cambiato in:

Hel lo non come volevo essere Hel su una linea e lo sulla linea segreta.

+0

Hai provato a fare la modifica in parola, ed esaminare le differenze? –

+0

Sì, ce l'ho, fa qualcosa come ... ma ho bisogno di usare il codice per il carattere di nuova riga, perché caricherò i dati dal DB e tutti i nomi caricherò vorranno avere ognuno su una nuova linea ... Spero tu capisca cosa intendo –

+0

Stai veramente modificando i file .docx? Come? (Non sono XML in quanto tali, ma XML compresso.) –

risposta

26

Utilizzare il tag <w:br/>.

L'ho trovato creando un documento di Word, salvandolo come XML (tramite Salva come), aggiungendo un'interruzione di riga forzata con Shift Invio e controllato la modifica. La differenza essenziale sembra essere solo il tag w:br, che apparentemente riflette il tag HTML br.

+0

Mi hai salvato un sacco di tempo! Grazie per la risposta! –

+0

Potrebbe sembrare ovvio, ma ciò che in realtà deve essere fatto è la sostituzione del tag ' insieme con' '... – Sebas

2

Nel caso in cui aiuta chiunque, il seguente pezzo di codice C# creerà la struttura XML multilinea

//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; 
    } 
} 

sopra creerà la necessaria nodi figlio e ritorni a capo il codice, e si prende cura del prefisso anche.

0

Sulla base @ risposta di Lenny sopra, questo è ciò che funziona utilizzando Obj-C nella mia situazione con MS Word 2011 su un Mac:

- (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; 
    } 
}