public Elem AppendElemValue(string InText) { // store the element value. if (mElemValue != null) { throw new ApplicationException("element already has a value"); } mElemValue = InText; // this element has a currently open sub element. Before adding // the value to the xml string, have to close the sub element. if (mCurrentSubElem != null) { mCurrentSubElem.CloseElem(); mCurrentSubElem = null; } if (mOpenUnitIsClosed == false) { AppendOpenUnitClose(); } string encodedValue = XmlStream.EncodeXmlString(mElemValue); mXmlString.AppendEncodedText(encodedValue); return(this); }
public Elem AppendAttribute(string InName, string InValue) { if (mOpenUnitIsClosed == true) { throw new ApplicationException( "xml elem open unit is closed. No more attributes allowed."); } mXmlString.AppendEncodedText(InName + "="); mXmlString.AppendEncodedText("\"" + XmlStream.EncodeXmlString(InValue) + "\" "); return(this); }
/// <summary> /// The element and its child elements in xml stream string form. /// </summary> /// <returns></returns> public override string ToString() { if (ValueForm == ValueForm.Attribute) { string encodedValue = XmlStream.EncodeXmlString(ElemValue); return(ElemName + "=\"" + encodedValue + "\""); } // only the top level root element is unnamed. else if (ElemName == null) { return(ToString_RootElem()); } else { return(ToString_NonAttributeElem()); } }
// An attribute in the xml element is the name=value string that follows the // xml element name. A non attribute elem does not have any such attributes. // ( the XmlElem class does not yet support attributes ) private string ToString_NonAttributeElem() { StringBuilder sb = new StringBuilder(); // start the xml elem. sb.Append("<" + ElemName + " "); // add the Attribute child elements to the string. bool nonNmvChildElems = false; foreach (XmlElem child in this) { if (child.ValueForm == ValueForm.Attribute) { sb.Append(child.ToString()); } else { nonNmvChildElems = true; } } // no value, no child elems. Close out this end node elem. if ((nonNmvChildElems == false) && (ElemValue == null)) { sb.Append("/>"); } else // close out the open unit. { sb.Append(">"); // followed by the element value if (ElemValue != null) { sb.Append(XmlStream.EncodeXmlString(ElemValue)); } // next, the non named value sub elements. if (nonNmvChildElems == true) { sb.Append(Environment.NewLine); foreach (XmlElem child in this) { if (child.ValueForm != ValueForm.Attribute) { sb.Append(child.ToString()); sb.Append(Environment.NewLine); } } if (sb.Tail(2) != Environment.NewLine) { sb.Append(Environment.NewLine); } } // the close unit. sb.Append("</" + ElemName + ">"); } return(sb.ToString()); }