Handling xml files with different structures and different names
I have a 开发者_C百科method and I want different users to pass me xml files. These files will have different names for the elements I am looking for and the elements I am looking for maybe at different structures. My first impression was that we should just tell them to pass in the xml in a standard format. However this is how they have their data and they insist that it is much easier if they don't have to convert it.
What can I do to take in data of all types?
Have them pass in a dictionary? number = mydata/numbers
What is the easiest way for them to define their data to me without actually changing it?
sample1
<numbers>
15
</numbers>
sample2
<mydata>
<mynumbers>
15
</mynumbers>
</mydata>
LINQ to XML can handle documents of varying format quite easily. For example this query will get the "15" for both the documents you mentioned:
XDocument doc = XDocument.Load("input.xml");
var result = doc.Descendants().Where(e => e.Name.ToString().Contains("numbers"));
精彩评论