2012-11-07 47 views
10

Ich habe MS Word Doc als .docx gespeichert. Ich möchte eine neue Zeile in meinen Text einfügen, indem ich die XML-Datei von docx edditing. Ich habe schon versucht 
, 
, 
, 	, und es gibt mir immer nur Platz keine neue Zeile.XML - Hinzufügen einer neuen Zeile

, was sie tut:

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

Wenn ich .docx Datei öffnen, dann wird es folgendermaßen geändert:

Hel lo nicht, wie ich Hel auf einer Linie sein wollte und lo auf der zweiten Linie.

+0

haben Sie versucht, die Bearbeitung in Wort zu tun, und die Unterschiede untersuchen? –

+0

Ja ich habe es so etwas wie ... aber ich muss Code für neue Zeilenzeichen verwenden, weil ich Daten von DB laden werde und alle Namen, die ich laden will, haben jedes auf neue Zeile ... Ich hoffe, Sie verstehen was ich meine –

+0

Bearbeiten Sie wirklich DOCX-Dateien? Wie? (Sie sind nicht XML an sich, sondern gezipptes XML.) –

Antwort

26

Verwenden Sie den Tag <w:br/>.

Ich fand es durch Erstellen eines Word-Dokuments, speichern Sie es als XML (über Speichern unter), Hinzufügen einer erzwungenen Zeilenumbruch mit Shift Enter, und checkte die Änderung aus. Der wesentliche Unterschied scheint nur das w:br-Tag zu sein, das anscheinend den HTML-Code br widerspiegelt.

+0

Sie haben mir viel Zeit gespart! Danke für die Antwort! –

+0

Scheint offensichtlich, aber was wirklich getan werden muss, ist das Ersetzen des '' Tags zusammen mit' '... – Sebas

2

Falls es jemand hilft, wird das folgende Bit von C# -Code die mehrzeiligen XML-Struktur

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

Der obige Code die notwendige untergeordneten Knoten und Wagenrücklauf wird erstellen erstellen, und kümmert sich um das Präfix auch.

0

Basierend auf @ Lennys Antwort oben, das ist, was mit MS Word 2011 in meiner Situation Obj-C arbeitet mit auf einem 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; 
    } 
} 
Verwandte Themen