I want to compare 2 web.config files by content, so that the difference is shown in terms of line number. I don't want to use Compare-Object as it compares line by line, which is not exactly the proper method for comparison.
Perhaps XmlDiff can help. It understands XML and generates a difference.
Have you considered to Serialize the config files? You can even generate classes with xsd.exe and use the objects in your program as you like.
You are looking for compare two web.config it means you wants two xml files, so you have to think that
You can use Xdocument object, Initialize two diffrunt XDocument object and load both xml respectivly
ref this answer
Related
First I load the file in a structure
XElement xTree = XElement.Load(xml_file);
Then I create an enumerable collection of the elements.
IEnumerable<XElement> elements = xTree.Elements();
And iterate elements
foreach (XElement el in elements)
{
}
The problem is - when I fail to parse the element (a user made a typo or inserted a wrong value) - how can I report exact line in the file?
Is there any way to tie an element to its corresponding line in the file?
One way to do it (although not a proper one) –
When you find a wrong value, add an invalid char (e.g. ‘<’) to it.
So instead of: <ExeMode>Bla bla bla</ExeMode>
You’ll have: <ExeMode><Bla bla bla</ExeMode>
Then load the XML again with try / catch (System.Xml.XmlException ex).
This XmlException has LineNumber and LinePosition.
If there is a limited set of acceptable values, I believe XML Schemas have the concept of an enumerated type -- so write a schema for the file and have the parser validate against that. Assuming the parser you're using supports Schemas, which most should by now.
I haven't looked at DTDs in decades, but they may have the same kind of capability.
Otherwise, you would have to consider this semantic checking rather than syntactic checking, and that makes it your application's responsibility. If you are using a SAX parser and interpreting the data as you go, you may be able to get the line number; check your parser's features.
Otherwise the best answer I've found is to report the problem using an xpath to the affected node/token rather than a line number. You may be able to find a canned solution for that, either as a routine to run against a DOM tree or as a state machine you can run alongside your SAX code to track the path as you go.
(All the "maybe"s are because I haven't looked at what's currently available in a very long time, and because I'm trying to give an answer that is valid for all languages and parser implementations. This should still get you pointed in some useful directions.)
I'd like to create a program that will go through a file (XML), find out a specific tag, will extract the integer from it and add a value to it, some like a replacement.
I know about the StreamWrite function but I'd need some help with the extracting-adding value-replacing thingy. I also would need to use the "\d+"
You can work with System.Xml.Serialization wich allow you to convert xml to objects and objects to xml and so you can do what you want.
More information
I have an XSD file and want to get a list of the names of all the elements in it. I don't mean stuff like <xs:sequence> and so on, just the "real stuff", that actually can appear in XML that are valid according to the XSD.
Real stuff is a bit vague
But if you just want want all elements it's just a it of Xpath.
If you want a tree, then you can't avoid sequence etc.
If you have things like xs:choice in there you have even more issues.
Then there's attributes...
From SimpleContent or ComplexType...
Might be easier to generate a 'blank' xml document from the xsd and then get what you want out of that. That's a fair chunk of code as well though. Might be one lying around you can borrow though.
If you don't actually want to do this from your code, you could use the XML Schema Definition Tool (Xsd.exe) to create source code for runtime objects.
From there you can use Xml serialization to create valid Xml samples for your given Xsd schema.
Since you're trying to code for this, I would assume you want to do this against different XML Schema files, over and over; if true, it would be then important to understand if you really have to embed this in your codebase, or if it can be used as an external tool.
If you really want to do it, most of all you need is in System.Xml.Schema package. Start with an XmlSchemaSet to load and compile your XSD files. Then using an iterator on GlobalElements, go over the global elements that can show as your root elements in XML document and traverse those (for what you need, use the PSVI properties); as someone else was mentioning, there will be types to go through, compositors, etc.; and then there's more: abstract elements (those can't show up in XML, neither references to abstract elements, instead members of substitution groups), prohibited attributes, restricted types, etc.
I've recently answered another post that may be related to your need; your posted XML Schema may look like this:
root/ship/engine/#MaxSpeed,A,1..1,True
root/ship/crew/#function,A,1..1,True
root/ship/#Name,A,1..1,True
root/ship/#class,A,1..1,True
root/ship/special_abilities/hull/#separable,A,0..1,False
root/ship/special_abilities/hull/#canCarryWesley,A,0..1,False
root/ship/special_abilities/hull/#capableOfLanding,A,0..1,False
If you want, you can deal only with the first column; the generated XPath shows only those items (elements or attributes) that have data; processing something like the above might be much easier (split the string using /, elements are all but #, etc.)
I am looking for some thing like .Net utility which allows you to export data/object to .csv and similarly read that data. Same like we have a concept of xmlSerialization.
Thanx
Have you tried SimpleCSV? (codeplex project)
I think it would be quicker and easier to write your own parser for this purpose. All you need to do is create a text file and append it with all the parameters of your object separated by a delimiter (";").
When you are reading it back in you will know the order of the parameters that are coming in and how they are separated so you can create a new object and convert the strings into their appropriate types and apply these values to your new object.
You can easily write your own serializer/parser using the String.Join and String.Split methods.
Is there a simple way to compare two XML structures to determine if they have the same structure and data?
I have a function that returns an XmlNode and I am trying to write unit tests for it. I store the correct XML result in a file. Durring the test I load the file into an XmlDocument, locate the proper XmlNode and compare against the result of the function. A straight compare does not work (as expected) and InnerXml does not work either.
I am considering removing all whitespace from InnerXml and comparing that, or writing my own compare to walk the tree, but I don't like either option much.
XNode.DeepEquals. Read the caveats before using it.
If you must use XmlDocument and its supporting types, consider using Microsoft's XmlDiffPatch, which performs customizable diff-operations on XML data structures.
Like CodeToGlory answered, XNode.DeepEquals() might fit your bill, check the remarks section on the MSDN page.
If you are stuck with XmlDocument (instead of XDocument), the answer is: No, there is no simple (existing way) to do it. XmlNode does not override Equals(), or provide an alternative. But it is not impossible to write, and that same Remarks section can be used as a starting point for a tree-walk algorithm.
Do get a clear picture of your requirements first, concerning Attributes, comments, CDATA nodes etc.