public String serializeNode(TreeElement instanceNode) { String ae = ""; // don't serialize template nodes or non-relevant nodes if (!instanceNode.isRelevant() || instanceNode.getMult() == TreeReference.INDEX_TEMPLATE) { return(null); } if (instanceNode.getValue() != null) { Object serializedAnswer = serializer.serializeAnswerData( instanceNode.getValue(), instanceNode.dataType); if (serializedAnswer is XmlElement) { // DON"T handle this. throw new SystemException("Can't handle serialized output for" + instanceNode.getValue().ToString() + ", " + serializedAnswer); } else if (serializedAnswer is String) { XmlDocument theXmlDoc = new XmlDocument(); XmlElement e = theXmlDoc.CreateElement("TreeElement");//TODO XmlText xmlText = theXmlDoc.CreateTextNode((String)serializedAnswer); e.AppendChild(xmlText); String tag = instanceNode.getAttributeValue("", "tag"); ae += ((tag != null) ? tag + delimeter : delimeter); // tag // might // be // null for (int k = 0; k < e.ChildNodes.Count; k++) { ae += e.ChildNodes[k].InnerText.ToString() + delimeter; } } else { throw new SystemException("Can't handle serialized output for" + instanceNode.getValue().ToString() + ", " + serializedAnswer); } if (serializer.containsExternalData(instanceNode.getValue())) { IDataPointer[] pointer = serializer .retrieveExternalDataPointer(instanceNode.getValue()); for (int i = 0; i < pointer.Length; ++i) { dataPointers.Add(pointer[i]); } } } return(ae); }
public XmlElement serializeNode(TreeElement instanceNode) { XmlElement e = theXmlDoc.CreateElement(instanceNode.getName());//TODO Name //don't serialize template nodes or non-relevant nodes if (!instanceNode.isRelevant() || instanceNode.getMult() == TreeReference.INDEX_TEMPLATE) { return(null); } if (instanceNode.getValue() != null) { Object serializedAnswer = serializer.serializeAnswerData(instanceNode.getValue(), instanceNode.dataType); if (serializedAnswer is XmlElement) { e = (XmlElement)serializedAnswer; } else if (serializedAnswer is String) { e = e.OwnerDocument.CreateElement(instanceNode.getName()); XmlText xmlText = theXmlDoc.CreateTextNode((String)serializedAnswer); //TODO name e.AppendChild(xmlText); } else { throw new SystemException("Can't handle serialized output for" + instanceNode.getValue().ToString() + ", " + serializedAnswer); } if (serializer.containsExternalData(instanceNode.getValue())) { IDataPointer[] pointer = serializer.retrieveExternalDataPointer(instanceNode.getValue()); for (int i = 0; i < pointer.Length; ++i) { dataPointers.Add(pointer[i]); } } } else { //make sure all children of the same tag name are written en bloc ArrayList childNames = new ArrayList(); for (int i = 0; i < instanceNode.getNumChildren(); i++) { String childName = instanceNode.getChildAt(i).getName(); Console.WriteLine("CHILDNAME: " + childName); if (!childNames.Contains(childName)) { childNames.Add(childName); } } for (int i = 0; i < childNames.Count; i++) { String childName = (String)childNames[i]; int mult = instanceNode.getChildMultiplicity(childName); for (int j = 0; j < mult; j++) { XmlElement child = serializeNode(instanceNode.getChild(childName, j)); if (child != null) { e.AppendChild(child); } } } } XmlElement e1 = e.OwnerDocument.CreateElement(instanceNode.getName()); e.ParentNode.ReplaceChild(e1, e); // add hard-coded attributes for (int i = 0; i < instanceNode.getAttributeCount(); i++) { String namespace_ = instanceNode.getAttributeNamespace(i); String name = instanceNode.getAttributeName(i); String val = instanceNode.getAttributeValue(i); e.SetAttribute(name, namespace_, val); } return(e); }