Reputation: 1866
I am getting error while updating component in Tridion using core service
Root element must be in namespace
My code:
ComponentData component = client.Read(webDavPath, readOptions) as ComponentData;
component = client.TryCheckOut(webDavPath, readOptions) as ComponentData;
//XDocument dom = XDocument.Parse(component.Content);
//// do your modifications to dom
//component.Content = dom.ToString();
doc.Load(filePath);
sw = new StringWriter();
xw = new XmlTextWriter(sw);
doc.WriteTo(xw);
component.Content = sw.ToString();
//client.CheckOut(webDavPath, true, readOptions);
client.Update(component, readOptions);
client.Save(component, readOptions);
client.CheckIn(component.Id, readOptions);
//client.Update(component, new ReadOptions());
// component = client.Update(component, readOptions) as ComponentData;
Upvotes: 2
Views: 1133
Reputation: 13483
Your schema has some namespace, for example here is my schema:
<xsd:schema elementFormDefault="qualified" targetNamespace="uuid:ce656a4c-71e8-407f-8734-26a60da2440a" xmlns="uuid:ce656a4c-71e8-407f-8734-26a60da2440a" xmlns:tcmi="http://www.tridion.com/ContentManager/5.0/Instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.tridion.com/ContentManager/5.0/Instance"></xsd:import>
<xsd:annotation>
<xsd:appinfo>
<tcm:Labels xmlns:tcm="http://www.tridion.com/ContentManager/5.0">
<tcm:Label ElementName="text" Metadata="false">text</tcm:Label>
</tcm:Labels>
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="Content">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="text" minOccurs="1" maxOccurs="1" type="xsd:normalizedString">
<xsd:annotation>
<xsd:appinfo>
<tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Notice "uuid:ce656a4c-71e8-407f-8734-26a60da2440a"
this is namespace of your schema. Root element of your component should be in the same namespace, here's corresponding component source:
<Content xmlns="uuid:ce656a4c-71e8-407f-8734-26a60da2440a">
<text>Some text</text>
</Content>
If namespace of your component is not correct - you will get exception like the one you have. If this doesn't solve your problem, could you please post your schema and component sources?
Upvotes: 2
Reputation: 1435
You need to specify the schema namespace when adding new fields to a component.
You can get the schema namespace in your code, and then use this when adding new fields.
You've not shown the code you are using to update your content, so its hard to show you in the context of your problem, but the example below may help. (it maybe slightly different for you, as you starting from an existing component)
// get namespace from component schema
SchemaData sd = client.Read(_componentSchemaTcmId, null) as SchemaData;
XNamespace ns = sd.NamespaceUri;
//create/update content
XElement contentXml = new XElement(ns + "news");
contentXml.Add(new XElement(ns + "title", "Title"));
contentXml.Add(new XElement(ns + "sub_title", "Sub Title"));
component.Content = contentXml.ToString();
Also, I don't think you need both client.Update(component, readOptions);
and client.Save(component, readOptions);
If this doesn't help then please post your full code.
Upvotes: 7