Difference between XMLDOM document, XML document and XML String document - c#

Can any one tell me what is XML String document and XMLDOM document? What will be the extension of those two documents (XML String and XMLDOM). How does those document differ from normal XML Document(.xml)?

XMLDOM document (which is actually XML DOM) or XML Document Object Model is an in-memory representation of an XML document. The DOM allows you to read, write, replace, manipulate and copy a XML document via code. For further reading, take a look here and here.
Never heard of XML String document, but i geuss you mean the type that represents the text written inside the XML document. Take a look here for more information.

Related

C# Get nodes inside CDATA of an xml file

I have an xml file which has a CDATA section, which has again xml data. I would need to get a specific node from the xml within CDATA and create one more node of the same type and save the xml.
The replace and save functionality works for 1 input. but i want the tag to be appended in the same file. I hope i am clear!
Have o look at this thread XML parsing : Reading CDATA You probably need to read the CDATA value, convert, create node and write it back

Convert all HTML entities not predefined for XML to unicode

I am trying to manipulate a string containing HTML-Code and then save the content to a htm-file. Afterwards the htm file is imported to a Word-File. Goal is to append a document formatted in HTML to a Word document. This process is part of a much larger programm and i cannot modify the given parameters.
To easily modify the HTML-Code I thought using XDocument would be a great idea.
So I tried this:
AppendContent(string content, Document doc)
{
string filePath = ...; //somewhere in /AppData/Local
var xDoc = XDocument.Parse(content);
// code left out because irrelevant
// Finding all "img" elements, in order to
// extract the embedded picture and save it as external file
FileHelper.SaveToFile(filePath, xDoc.ToString());
//... After this, the file is appended to the word file (the one in doc)
}
First attempt worked actually, with a small test html. Using any of the big documents I'm trying to append to the word document, cause an exception to be thrown:
XDocument.Parse cannot parse entities like "nbsp" or "uuml" (german ü). I already found out that XML only supports a hand full of predefined entities, so i would have to manually add the definition to the html file. This is not an option, because this operation is supposed to work with ANY Html file.
I found following fix:
var decodedContent = WebUtility.HtmlDecode(content);
var xDoc = XDocument.Parse(decodedContent);
This converts all entities to the representing character. So "uuml" is converted to "ü", etc. This worked until i hit a document that contained the "amp" entity, which is then converted to "&"... and such the XDocument.Parse is complaining again.
I'm looking for a way to convert HTML to unicode-representation ("\0x1234") or a HTML-decode, that does not decode XML-predefined entities.

XmlReader - read xml with colons in tag names

I have very complex XML that I need to read with XmlReader.
The elements in the xml are as the following:
<log:event>
<ev:logger>some text</ev:logger>
<ev:line>24</ev:line>
<ev:ex>
<ev:levelone>some message</ev:levelone>
<ev:leveltwo>some other message</ev:leveltwo>
</ev:ex>
</log:event>
XmlReader will not know how to read this since it does not have name space definition on each xml tag.
I would have done that programmatically (appending namespace to the strings), but the file is huge and I its impossible.
(I dont control the file creation).
Any suggestion how can that file be read as xml without namespacing?
Thanks!
You can still append namespaces, just read the file into memory and manipulate it there. I do it with several XML-based API's from machine manufacturers that doesn't comply with XML standards to make it easier to read with normal xml parsers

C# XML conversion

I have a string containing fully formatted XML data, created using a Perl script.
I now want to convert this string into an actual XML file in C#. Is there anyway to do this?
Thanks,
You can load a string into an in-memory representation, for example, using the LINQ to SQL XDocument type. Loading string can be done using Parse method and saving the document to a file is done using the Save method:
open System.Xml.Linq;
XDocument doc = XDocument.Parse(xmlContent);
doc.Save(fileName);
The question is why would you do that, if you already have correctly formatted XML document?
A good reasons that I can think of are:
To verify that the content is really valid XML
To generate XML with nice indentation and line breaks
If that's not what you need, then you should just write the data to a file (as others suggest).
Could be as simple as
File.WriteAllText(#"C:\Test.xml", "your-xml-string");
or
File.WriteAllText(#"C:\Test.xml", "your-xml-string", Encoding.UTF8);
XmlDocument doc = new XmlDocument();
doc.Load(... your string ...);
doc.Save(... your destination path...);
see also
http://msdn.microsoft.com/fr-fr/library/d5awd922%28v=VS.80%29.aspx

Creating XSD Dynamically

I have two inputs. I get as input one XML file. I have to create an XSD file for this XML file. This XML file has tags which depend on another input. But that XML file should have certain tags for sure. For example, the XML file has the following structure :
<A>
<B>
<C>...</C>
<D>...</D>
<E>
<F>...</F>
<G>...</G>
</E>
</B>
</A>
Here, in this XML file, A,B and E tags should be compulsory. But the tags C and D inside the B tag and tags F and G inside the E tag depends on another input. So I should create an XSD dynamically(i know that A,B and E tags should be present and I do know about the other tags from the other input) and validate the input XML file against the XML Schema. Can someone tell me how I can do this in C#?
I have no idea what you're asking.
An XSD is a blue-print for constructing a business-valid XML document. You do not generally create XSD documents dynamically. You obtain an XSD document so that you can create an XML document that will be valid in a specific business usage or validate XML documents against that schema.
I'm know XML Serialization in C# is covered in great depth on the web.
Have you looked at XSLT yet? It's very useful for creating one XML file based on another. If you can access an XSLT engine from C# (I guess that's possible), I can help you set up the XSLT stylesheet.

Categories

Resources