3

In the Metadata Design of a Component I have the following structure as displayed in the image below:

enter image description here

Inside my Event System, I am fetching the component.Metadata.InnerXml, the XML returned shows as follows:

<metadata xmlns="TVShow">
    <activation_date>2012-06-03T11:31:42</activation_date>
    <expiration_date>2014-06-03T11:31:48</expiration_date>
    <notes>test</notes>
</metadata>
<distribution_start_date xmlns="TVShow">2013-08-13T10:00:51</distribution_start_date>
<distribution_end_date xmlns="TVShow">2015-08-13T10:00:58</distribution_end_date>

Which is breaking my code as I'm expecting this XML to only contain one root element. Therefore I assume the XML is not well formed. What can be the resolution?

3

Isn't that what "inner" XML means? Anyway - this situation is quite common. The usual solution is simply to add your own element to contain it.

  • That is right meaning of inner xml. Yes I can add my own element. – AlokBhatt Aug 13 '13 at 7:19
3

Component.Metadata returns an XmlElement, so on there you should have both an InnerXml and an OuterXml property.

The InnerXml property returns you the markup representing just the children of this node as a string. That is well formed XML, but cannot be considered an XmlDocument. If you want to process it as an XmlDocument, you will either need to wrap it inside a root element, or use the OuterXml property.

Depending on what you want to do with the contents, you can also use an ItemFields Collection like this:

ItemFields metadata = new ItemFields(component.Metadata, component.MetadataSchema);
if (metadata.Contains("distribution_start_date"))
{
    DateField distributionStartDateField = (DateField) metadata["distribution_start_date"];
    DateTime distributionStartDate = distributionStartDateField.Value;
}
1

Use OuterXml property and not InnerXml. InnerXml is not showing the node itself

  • I am not seeing any such property "component.Metadata.OuterXml" – AlokBhatt Aug 13 '13 at 7:17
1

Here is how I did it.

if (component.Metadata["distribution_start_date"] != null)
{
    distributionStartDate = Convert.ToDateTime(component.Metadata["distribution_start_date"].InnerText);
}
if (component.Metadata["distribution_end_date"] != null)
{
    distributionEndDate = Convert.ToDateTime(component.Metadata["distribution_end_date"].InnerText);
}

if (component.Metadata["metadata"]["activation_date"]!=null)
{
    activationDate = Convert.ToDateTime(component.Metadata["metadata"]["activation_date"].InnerText);
}
if (component.Metadata["metadata"]["expiration_date"]!=null)
{
    expirationDate = Convert.ToDateTime(component.Metadata["metadata"]["expiration_date"].InnerText);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.