score:0
When you get XML data with irregular structure; not naturally fitting a DataSet and you want an Object Model to easily work with the data. You can use the XML Schema Definition Tool (Xsd.exe) with the /classes option to generate C# or VB.Net classes from an XML file.
The XSD.exe lives in :
C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\xsd.exe
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe
You run xsd.exe from the Visual Studio Command Line.
-Start
-All Programs
-Visual Studio
-Tools
-Command Line
This is the command to view all the XSD command line parameters:
xsd /?
To convert an irregular XML file (XmlResponseObject.xml) into Classes:
xsd c:\Temp\XmlResponseObject.xml /classes /language:CS /out:c:\Temp\
This will generate a csharp file with classes that represent the XML. You may want to refeactor it out into separate class files being careful about duplicate classes in the single file that are disambiguate by namespace. Either way the classes wont be the nicest looking with all the xml attributes but the good part is you can bind to them via XML. This is an example where I retrive XML via a REST webservice, xmlResponseObject is the ObjectModel of classes that fits the XML.
public interface IYourWebService
{
XmlResponseObject GetData(int dataId);
}
public class YourWebService : IYourWebService
{
public XmlResponseObject GetData(int dataId)
{
XmlResponseObject xmlResponseObject = null;
var url = "http://SomeSite.com/Service/GetData/" + dataId;
try
{
var request = WebRequest.Create(url) as HttpWebRequest;
if (request != null)
{
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322; InfoPath.2; .NET4.0C; .NET4.0E)";
request.Credentials = CredentialCache.DefaultNetworkCredentials;
request.CookieContainer = new CookieContainer();
var response = request.GetResponse() as HttpWebResponse;
if (request.HaveResponse && response != null)
{
var streamReader = new StreamReader(response.GetResponseStream());
var xmlSerializer = new XmlSerializer(typeof(XmlResponseObject));
xmlResponseObject = (XmlResponseObject)xmlSerializer.Deserialize(streamReader);
}
}
}
catch (Exception ex)
{
string debugInfo = "\nURL: " + url;
Console.Write(ex.Message + " " + debugInfo + " " + ex.StackTrace);
}
return xmlResponseObject;
}
}
Given you wish to only send and receive document changes you could modify the classes with IsDirty flags. I'm sure though once you have the classes to work with, it will be dead easy to detect diff's.
score:0
To load any XML data into DataSet
, you have to provide corresponding schema.
See Deriving DataSet Relational Structure from XML Schema (XSD).
Besides, DataSet
/DataTable
doesn't work with XML documents. They can import data from, and export data to XML.
score:0
I haven't found any useable answers anywhere. It seems back in 2003 MS was talking about creating an XPathDocument2 or something that implemented what I'm asking for (books talking about the coming release mention it), but it doesn't seem to have been carried out. So here's my attempt at a solution:
Use XPathDocument/XPathNavigator, and add event handlers for Change/Delete/Insert. For each of these events, put a record in a DataTable {XPath | OldValue | NewValue} indicating the change. When ready to Commit, send the table across then clear it. If instead cancelling, use the Table info to undo the changes in the XPathDocument.
I haven't implemented this yet, but it seems like it might serve.
score:0
I have tried to find a free or open-source XML diff tool numerous times before, but never dug up anything that really fit the bill. Essentially, you're looking at tree diffing, which is a whole discpline on its own. The fact that you're using XML is subordinate to this, I guess, as it's nothing but a tree in another form. You "just" need to define what specifies a node.
Though the Decomposition Algorithm for Tree Edit Distance calculates the distance between 2 trees, I suspect you can transform it to give you all changes, as that's the base for the distance measurement. How you communicate the changes after detection, is completely up to you. That could range from XML to JSON. Note that the authors of the algorithm mention they created a Python version in a few dozens of lines, so maybe if you drop the a line, they can be of assistance.
It looks like you could be the first one to publish a practical proof of concept if you can get this done :)
score:0
The problem you have here is that XML is just a form of representing data, its not necessarily the data itself. Is this some sort of XML editor you are using, or is XML just the transport?
If you are talking about xml as a transport then when you talk about sending XML changes descriptions, you probably want to be generating those change descriptions at the point you generate the change itself, and there is every chance that the change descriptions won't be in the same schema that the original data is.
In addition the reason that datasets can do this, is because each row in a dataset has a known unique key. So the change can be sent back for the row instead of the entire set. XML doesn't work like that, each row doesn't have a unique key. XPath can be used as the change locator but that could be more inefficient than sending the entire document with enough edits.
Why not simply treat the XML as text as use anyone of the standard patching algorithms? (look at the source for Git or Hg)
score:1
Do you need to act on this changes or just store them, if you want just to store the updated version you can use binary diff algorithms to pass the diffs between 2 xml files. And then to updated stored version with the difference. Good algorithm for this is bifdiff The C# version can be found here.
Another aproach is to use this XmlDiff class from MS
score:1
- How do you suppose to only send changes?
- Do you expect numerous changes or just slight changes every time?
- What kind of changes do you have to consider?
- Are you trying to maintain to copies of the same document across process boundaries?
- How are you going to resolve conflicting changes?
- Are you going to lock xml documents until changes are propagated?
- Are both copies independent, or one is master copy?
if you used XmlDocument events such as NodeInserted, NodeDeleted, NodeChanged you could build a list of such changes and then execute them on another copy. If total amount of changes is longer than document itself you could send document instead. Zipping xml data also helps.
other than that I do not see any other easy approach.
Source: stackoverflow.com
Related Query
- Is there a way to manage changes to an irregular XML document like there is with DataTable?
- Is there a way to speed up this code that finds data changes in two XML files?
- Is there any way to make Code Contracts work with LINQ?
- LINQ to SQL - is there a way to get a 'preview' of the database with changes made to a context?
- C# parsing xml document with LINQ where there are varying number of duplicate elements
- Is there a better way to achieve this with Linq or a better code
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ: is there a way to supply a predicate with more than one parameter to where clause
- Better way to cleanly handle nested XML with LINQ
- Is there an efficient way to do a selection statement with two variables?
- Reading an XML document with Linq
- Is there a more clear and concise way to express this statement with better LINQ?
- When selecting an anonymous type with LINQ from EF, is there no way to run a method on an object as you select it?
- Is there a way to return only the function calls that do not throw exceptions with LINQ?
- Is there an easy way to do parallel aggregation with a non-commutative operation?
- Is there a way to use linq in C# application with .net framework 2?
- Is there a way to merge a collection of duplicated items along with their child collections using LINQ?
- Group and sort elements in an XML document with LINQ in C#
- Is there a faster way to check for an XML Element in LINQ to XML?
- Is there a better or more efficient way to filter with linq
- Is there a way to use PredicateBuilder with Azure Table Storage?
- Is there a better way to do this, maybe replace the for/foreach loop with something LINQ ish?
- Is there a better way to search a list with another list
- Is there a way to order by an IQueryable with a different amount of ordering
- Is there any way to ignore column of an entity from cassandra database with .net driver
- Searching Xml Document with linq doesn't return anything
- Is there a faster way to check for an XML Element in LINQ to XML, and parse a bool?
- Update IEnumerable<XElement> and save changes in XML document C#
- Can i do this with dynamic LInq or is there a better way - Linq, C#, VS2008
- What is the correct way to Take() X-number of records with LINQ but find out if there are more
More Query from same tag
- Why does Entity Framework 6 generate complex SQL queries for simple lookups?
- Get xml attribute value from an associated attribute value
- Passing Enum Array or list to Function
- LINQ Group Rows Into Columns
- linq weekly grouping?
- Can I put a list of a reference object in the KeySelector in a GroupBy?
- How would I add type specific implementations to linq binary expressions?
- Read XML with Xdocument
- Combining Filtering and string concatenation with DRY
- Entity Framework ORDER BY issue
- LINQ - Generate identity for object
- How to reuse LINQ Select Expression with arguments
- Stored Proc in RIA Services
- LINQ Distinct Not Working
- Select inside linq select
- How use of select for associations in linq to NHibernate
- Perform a linq expression for 'contains' with searching through a list for 'like' not exact matches
- How to write an efficient LINQ query for a self-referential many-to-many relationship?
- Getting error The 'proprrty1' property on 'table1' could not be set to a 'System.Int64' value. in my mvc application
- Edit Object with xml without creating new Instance
- Is it possible to group LINQ elements based on a sequence?
- Parsing Conditional Expressions to String
- LINQ group by and select random element from each group
- "Cannot call methods on DateTime", and other limitations
- LINQ to XML extract based on condition
- How do I get Distinct() to work with a collection of custom objects
- Making sure a value is equal to one of many in LINQ
- Optimize solution for categories tree search
- Using LINQ to flatten a hierarchical dataset - with a caveat
- Data Annonation Lost in Joined Linq Select