I'm trying to replace a text element placeholder with an image in an openXML docx.
I've found a tutorial here which seems to do what I need, but I'm not quite following what he does to insert the image.
Basically, I have an XML 'image template' stored in a string. I can store my image to media folder and insert the image ID into the XML string:
string imageNode
= _xml.Replace("##imageId##", documentMainPart.GetIdOfPart(newImage));
so now I have the correct XML as a string which I need to insert into the document.
I can find my placeholder text node which I want to replace with the new image XML
var placeholder = documentMainPart.Document.Body
.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>()
.Where(t => t.Text.Contains("##imagePlaceholder##")).First();
But this is where I get stuck. I can't see how to do a replace/insert which will take an XML string. I've managed to get my XML output as text in the document, but I beed to somehow convert it into an XML element.
If you are asking how you import the XML that displays the image then it shouldn't be a big problem.
How you store the image I'm not sure though, but I guess you will have to import it with a proper name somewhere inside the .docx but I'm assuming you know this by reading your post.
Replacing the placeholder with the image xml thingy is easy
var parent = placeholder.Parent;
parent.ReplaceChild(imageXML, placeholder);
Here you are actually replacing the image thingy with the text tag but I can't be sure how that would work. I know that a Image could be within a run tag wich I assume is the parent of your text tag.
Now if your XML you get form your command is correct you should be OK. It should be Drawing/Inline/Graphic root I think.
Please comment If I'm misunderstanding your question
To convert your string representation to an xml node belonging to the xml document, use XmlDocument.CreateFragment:
XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
docFrag.InnerXml = imageXML;
placeholder.Parent.ReplaceChild(docFrag,placeholder);
Related
My Program: Contains two textBoxes and one XML file on the desktop.
XML File:
My Goal: I want to parse XML File and display the desired values in the textBox. First number 123456 from XML File will be displayed in the first texBox. Similarly, Second number 9876 from XML File will be displayed in the second texBox. Rest, everything should be ignored in the XML File.
I tried to Google search for a simple example that will help me understand how to parse XML file and display in textbox but I couldn't find any such simple program example. This program will help me learn this technique quickly and easily, and your help will be much appreciated! Thanks!! :)
var dict = XDocument.Load(fname)
.Descendants("field")
.ToDictionary(f => f.Attribute("name").Value,
f => f.Attribute("value").Value);
firstNumberTextBox.Text = dict["first_number"];
secondNumberTextBox.Text = dict["second_number"];
I am reading an XML string with XDocument
XmlReader reader = XmlReader.Create(new StringReader(xmltext));
reader.Read();
XDocument xdoc = XDocument.Load(reader);
Then I grab the content of some tags and put them within tags in a different string.
When I try to Load this string in the same way I did with the first, I get an error "An error occurred while parsing EntityName. Line 1, position 344.".
I think it should be parsed correctly since it has beem parsed before so I guess I am missing something here.
I am reading and copying the content of the first XML with (string)i.Element("field").
I am using .net 4
When I grab the content of the xml that I want to use for building another Xml string I use (string)i.Element("field") and this is converting my Xml into string. My next Xml Parsing does not recognize it as an Element anymore so I solved the problem by not using (string) before I read my element, just i.Element("field") and this works.
It sounds like you've got something like this:
<OriginalDocument>
<Foo>A & B</Foo>
</OriginalDocument>
That A & B represents the text A & B. So when you grab the text from the element, you'll get the string "A & B". If you then use that to build a new element like this:
string foo = "<Foo>" + fooText + "</Foo>";
then you'll end up with invalid XML like this:
<Foo>A & B</Foo>
Basically, you shouldn't be constructing XML in text form. It's not clear what you're really trying to achieve, but you can copy an element from one place to another pretty easily in XElement form; you shouldn't need to build a string and then reparse it.
So after spending hours on this issue:
it turns out that if you have an ampersand symbol ("&") or any other XML escape characters within your xml string, it will always fail will you try read the XML.
TO solve this, replace the special characters with their escaped string format
YourXmlString = YourXmlString.Replace("'", "'").Replace("\"", """).Replace(">", ">").Replace("<", "<").Replace("&", "&");
I want to save the following string in an XML File:
<text><![CDATA[<p>what is my pet name</p>]]></text>
When I am saving it, it looks like:
<text><![CDATA[<p>what is my pet name</p>]]></text>
I have tried File.WriteAllText(), XmlDocument.Save() methods but didnt get the proper response.
basically everywhere other than opening and closing tags in the XML, < is replaced by < and > is replaced by >.
What is happening is that the XML parser is encoding your string. When you try to access the string later, it can be decoded again at that time.
What I suggest, is that you either try to load the text as into a new 'XmlDocument' with XmlDocument.LoadXml(string s), and then import that into your current document, or leave it encoded.
You should not try to both use an XML parser, and manually add text at the same time.
I guess you add the CDATA manually and the XML writing mechanism correctly escapes your CDATA because it treats it as text content. Instead explicitly add a CDATA section with just the contents.
If you are using the old XML API (System.XML), then use this method to create the CDATA Section: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection
Then append the node to the element just like in the example in the link.
XML is being written correctly.
XML has special characters that are reserved for commands, just like C# reserves words like "if" and "string".
XML is encoding your string for storage. What you need to do is when you retrieve your string, run it through a similar decode process.
Use this: HttpServerUtility.HtmlDecode(encodedString)
Reference:
Decode XML returned by a webservice (< and > are replaced with < and >)?
Hi and thanks for looking!
Background
I am working on a developer tool for our dev team that parses content from MS Word into a Windows form with text boxes. We do some processing on the text, then submit the form to a database.
Some of the textboxes in the form contain Word XML which we need to clean up and convert to our own XML to later use with XSLT.
When the form populates, I would like to take the Word XML and use Linq to search for certain tags (example: <w:t>SOME TEXT</w:t>) and convert it to our own XML (<Text>SOME TEXT</Text>) before it gets to the textbox.
Question
How do I use Linq-to-Xml on the contents retrieved from a string in the pre-processing stage? I know how to instantiate an XDocument, but this is just a string so I am stumped. Probably missing something simple.
Thanks!
You can use the XDocument.Parse Method to create an XDocument from a string.
I've created an application in c# where an image is inserted into xml by turning it into bytes. How do i then convert this image from the xml into a word document?
This article might help you:
Inserting images into Word documents using XML
The main idea of the article is to construct a WordML (WordProcessingML) document fragment representing the image to be inserted and then calling Word's InsertXML function to place the image in the document.