I am a newbie in C#. I have doubt regarding the transformation of excel sheet to XML in c#. I am currently in the part where I have some cells, that I have to access and print corresponding XML nodes to them in the XML file that I have created. For eg: If the cell A1 is there, it should print <abc> node. This has to be hardcoded. I want to do this by LINQ to XML, using XElement and XDocument. Any help would be appreciated.
Related
I am a beginner in c#, so don't expect a lot from me...
How can I transfer nodes from one xml file to another and store it in the new xml file using c#?
The procedure I took was the following:
Load 2 xml files by using variablename.load(filepath)
Creating XmlElements which store root node of the imported xml-files.
Nested for-loop across the nodes in the xml files.
If nodes of the documents are the same, don't do anything
Else: Remove the content of the previous XML file and update it with the content of the new XML-file
Apparently implementing appendChild results in a System.NullReferenceException: error
I have a word document (.xml file) and I need to convert the tables to html. Is there some kind of existing tool for c#? The way I get the xml is from the document is:
Table table = element.Descendants<Table>().First();
string ttt = table.InnerXml;
The Open XML PowerTools have the functionality to transform Word documents into HTML. Note, though, that they are not using the strongly-typed classes of the Open XML SDK but rather the Linq to XML (e.g., XElement) classes.
BTW, you would never use the inner XML to transform an element but rather the outer XML, which includes the w:tbl element in your case.
I am new to C# and got a question. I am writing automation test for SOAP web service using HttpWebRequest and I am passing XML to request and getting XML back as string.
Now, I am trying to pass this request XML saved in file but wants to change XML node values. I am thinking of having different test data from an Excel sheet and then passing that data to XML nodes.
I can do this if I saved all XML request in a string object within a class but I want to read request from XML file.
I am not sure how to do that.
There are different ways to parameterize XML and send to a webservice. Easiest way is to create a template XML with placeholders and then at run-time replace the placeholders with data from excel, as shown below.
XML:
<Name>
<FirstName>{{FirstName}}</FirstName>
<LastName>{{LastName}}</LastName>
</Name>
Excel/CSV
FirstName,LastName
FName1,LName1
FName2,LName2
You can now read the values from the excel as a dictionary and do something like below.
C# Code Illustration:
foreach(KeyValuePair kvp in dictionary)
{
xml = xml.Replace("{{" + kvp.key + "}}", kvp.value)
}
If the XML parameterization is not feasible, you can navigate to the XML nodes using XPath (Use any of the XML holder objects) and set the node values.
How can I discriminate the elements inside a table and those outside? And additionally how can I verify tables without a content control name?
I suggest you use Linq To XML. On MSDN there is an example console application that displays all paragraph text of a Word Document.
Near the bottom is a comment - Find all paragraphs in the document - this is the Linq To XML piece that pulls out the paragraphs from the body of the Word document.
// Find all paragraphs in the document.
var paragraphs =
from para in xDoc
.Root
.Element(w + "body")
.Descendants(w + "p") ...
Instead of a "p", you will need to use "tbl". This is how to collect all of the tables from a Document in order to verify their contents. To inspect each row and column will involve more code to loop through the tables data, but this should get you started.
If you install the Open XML Productivity Tool, you can view all of the xml of any Open XML document. The screen below shows the tool with a Word doc containing a table.
[]
The left pane show the structure of a typical table in a Word doc. The right is the Open XML Table spec. The tool helps you know what to read and what to ignore when you are writing your liq to xml code to read and verify the data in your tables.
If you have a specific table format you need to read for your project and you are stuck, post the table and the code you tried in another question. Otherwise based on your original question, this answer should be enough to help you get started towards your solution.
I have a source xml which needs to be compared against xml template(another xml file).
The following needs to be implemented,
The order of the source xml needs to be transformed as per the order in the template xml.
If the elements in the template xml are not found in the source xml then it should be added.
Basically what should I need to do this, compare 2 xml's and need to change one xml based on other.
I have an idea of creating XDocument with 2 xml's and by accessing xpath of source xml from template xml, Rearrange the elements and add elements if not found.
Please advise whether this is a better approach, or anything can be added into this?