I have an XML file, I need to extract values from it, and put them in another XML file.
Questions:
-
Another person is creating the “schema” for the resulting XML file. Is there something that person can give me that will automate the inserting of the values? Do I even need to extract anything from the XML, or can something like a XSLT just do all the transformation?
-
Is there a problem with this XML structure below? I tried using xsd2code to generate objects but nothing will load when I use the LoadFromFileMethod – I read an article that wasn’t very specific but said “nested parents” cause problems for XSD.exe and xsd2code.
<Section> <Form id="1"...> <Control id="12523"..> <--Some have this some don't <Property name="Color">Red</Property> <Property name="Size">Large</Property> </Control> </Form> <Form id="2"...> <Property name="Color">Blue</Property> <Property name="Size">Large</Property> </Form> <Form id="3"...> <Property name="Color">Red</Property> <Property name="Size">Small</Property> </Form> </Section>
Thank you for any guidance!
XSLT is the tool for XML transformation.
As far as your XML goes, in a lot of applications you should replace this:
with:
Some reasons:
If you want to write a schema that restricts an element’s content in some way (e.g. to one of a list of values), the element must be identifiable by its name; you can’t write one schema for a
Propertyelement with anameattribute equals “Color” and another schema for aPropertyelement whosenameattribute equals “Size”.It’s easier to write XPath predicates if your element names are meaningful. For instance,
Form[Color = 'Red']is a lot easier to write (and read) thanForm[Property[@name='Color' and .='Red']]The above is also true if you’re writing Linq queries against the XML, in pretty much the same manner. Compare
Element.Descendants("Color")withElement.Descendents("Property").Where(x => x.Attributes["name"] == "Color").There are applications where it’s appropriate to have generically-named elements, too; the above argument’s not definitive. But if you’re going to do that, you should have good reasons.