Skip to content
JPVenson edited this page Apr 2, 2023 · 3 revisions

You can add support for XDocument directly to Morestachio by using the extension method XmlDocumentValueResolverExtensions.WithXmlDocumentValueResolver within the Nuget Morestachio.System.Xml.Linq nuget package.

When enabled it adds support for XDocument and all other objects that inhert from XContainer.

There are some differences to the Json Value providers as XML does not any provide one way to provide lists of data. Morestachio handles all elements that have the same local name as a list of objects selector so:

<Root>
	<Data>
		<PropA>E</PropA>
		<PropA>T</PropA>
		<PropA>e</PropA>
	</Data>
	<PropB>st</PropB>
</Root>

will be recognized by morestachio as having the objects Root, Data and Data having a list of PropA. That allows you to access a list of PropA by calling:

{{#FOREACH prop IN Root.Data.PropA}}...

Attributes and contents of xml elements can be accessed by using the path to it directly so:

<Root>
	<Data Value="test">
		<PropA>E</PropA>
	</Data>
	<PropB>st</PropB>
</Root>

The Value attribute can be then accessed by {{Root.Data.Value}}. When accessing an xml element, its contents will be printed as-is including children in xml format. So taking the same structure and calling {{Root.Data}} will return:

		<PropA>E</PropA>

WithXmlDocumentValueResolver also adds some formatters for accessing the XML element directly. This is helpfull if you have an element and attribute named the same like this:

<Root>
	<Data PropA="test">
		<PropA>E</PropA>
	</Data>
</Root>

You can use either the Root.Data.GetAttribute("PropA") or the Root.Data.GetElement("PropA") to access a ambiguines named element.