[OpenXml] - les deux points dans les balises

Résolu
romagny13 Messages postés 687 Date d'inscription lundi 10 janvier 2005 Statut Membre Dernière intervention 27 août 2014 - 19 avril 2007 à 12:44
romagny13 Messages postés 687 Date d'inscription lundi 10 janvier 2005 Statut Membre Dernière intervention 27 août 2014 - 19 avril 2007 à 13:30
Bonjour,

voila j'ai un problême lorsuqe je génére le fichier document.xml (OpenXml)
les balises sont de la forme <w:p> ou <w:r> par exemple

si j'utilise un XmlWriter > j'ai un message d'erreur
Caractère de nom non valide dans 'w:document'. Le caractère ':', valeur hexadécimale 0x3A, ne peut pas être inclus dans un nom.

si j'utilises un XmlDocument il enleve les w: et mes balises générées sont de la forme  ou <r> au lieu de <w:p> et <w:r>

comment faire pour conserver la balise telle qu'il faut la générer (avec donc w: )  ?
(que ce soit avec un XmlWriter ou XmlDocument)

2 réponses

romagny13 Messages postés 687 Date d'inscription lundi 10 janvier 2005 Statut Membre Dernière intervention 27 août 2014 3
19 avril 2007 à 13:30
Bon j'ai peut être trouvé en fait apparemment il faut obligatoirement indiquer le namespace pour chaque element :

System.IO.Packaging.

PackagePart oPackagePart = oPackage.CreatePart(oUri,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

string WordprocessingML =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main" ;

//// 1 avec XmlDocument

using (System.IO.
Stream oStream = oPackagePart.GetStream()){

System.Xml.

XmlDocument oXmlDocument;oXmlDocument =

new System.Xml.
XmlDocument();System.Xml.

XmlElement oXmlElementDocument = oXmlDocument.CreateElement(
"w:document", WordprocessingML); System.Xml.

XmlElement oXmlElementBody = oXmlDocument.CreateElement(
"w:body", WordprocessingML);System.Xml.

XmlElement oXmlElementParagraph = oXmlDocument.CreateElement(
"w:p", WordprocessingML);System.Xml.

XmlElement oXmlElementRun = oXmlDocument.CreateElement(
"w:r", WordprocessingML); System.Xml.

XmlElement oXmlElementText = oXmlDocument.CreateElement(
"w:t", WordprocessingML);System.Xml.

XmlText oXmlText = oXmlDocument.CreateTextNode(
"ouiiiiiiiiiii");oXmlDocument.AppendChild(oXmlElementDocument);

oXmlElementDocument.AppendChild(oXmlElementBody);

oXmlElementBody.AppendChild(oXmlElementParagraph);

oXmlElementParagraph.AppendChild(oXmlElementRun);

oXmlElementRun.AppendChild(oXmlElementText);

oXmlElementText.AppendChild(oXmlText);

oXmlDocument.Save(oStream);

oPackage.Flush();

oPackage.Close();

}
3
romagny13 Messages postés 687 Date d'inscription lundi 10 janvier 2005 Statut Membre Dernière intervention 27 août 2014 3
19 avril 2007 à 12:54
Voila un exemple de code cela vous aidera peut etre à m'aider :p lol

// 1 avec XmlDocument

using (System.IO.
Stream oStream = oPackagePart.GetStream()){

System.Xml.

XmlDocument oXmlDocument;oXmlDocument =

new System.Xml.
XmlDocument();System.Xml.

XmlElement oXmlElementDocument = oXmlDocument.CreateElement(
"w:document");System.Xml.

XmlElement oXmlElementBody = oXmlDocument.CreateElement(
"w:body");System.Xml.

XmlElement oXmlElementParagraph = oXmlDocument.CreateElement(
"w:p");System.Xml.

XmlElement oXmlElementRun = oXmlDocument.CreateElement(
"w:r");System.Xml.

XmlElement oXmlElementText = oXmlDocument.CreateElement(
"w:t");System.Xml.

XmlText oXmlText = oXmlDocument.CreateTextNode(
"ouiiiiiiiiiii");oXmlDocument.AppendChild(oXmlElementDocument);

oXmlElementDocument.AppendChild(oXmlElementBody);

oXmlElementBody.AppendChild(oXmlElementParagraph);

oXmlElementParagraph.AppendChild(oXmlElementRun);

oXmlElementRun.AppendChild(oXmlElementText);

oXmlElementText.AppendChild(oXmlText);

oXmlDocument.Save(oStream);

oPackage.Flush();

oPackage.Close();

}

 

//2 avec XmlWriterSystem.Xml.

XmlWriter oXmlWriter;System.Xml.

XmlWriterSettings oXmlWriterSettings; 

oXmlWriterSettings =

new System.Xml.XmlWriterSettings();oXmlWriterSettings.Indent true;oXmlWriterSettings.Encoding System.Text.

Encoding.UTF32;oXmlWriter = System.Xml.

XmlWriter.Create(oPackagePart.GetStream(), oXmlWriterSettings);oXmlWriter.WriteStartElement(

"w:document");oXmlWriter.WriteStartElement(

"w:body");oXmlWriter.WriteStartElement(

"w:p");oXmlWriter.WriteStartElement(

"w:r");oXmlWriter.WriteElementString(

"w:t",
"ouiiiiiiiiiiiiiiii");oXmlWriter.WriteEndElement();

oXmlWriter.WriteEndElement();

oXmlWriter.WriteEndElement();

oXmlWriter.WriteEndElement();

// rootoXmlWriter.Close();
0
Rejoignez-nous