public void Export(RbfStructure value, string xmlFileName) { using (var xmlFileStream = new FileStream(xmlFileName, FileMode.Create)) { Export(value, xmlFileStream); } }
public void Export(RbfStructure value, Stream xmlFileStream) { var writer = new XmlTextWriter(xmlFileStream, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement(value.Name); WriteStructureContentXml(value, writer); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); }
public void Export(RbfStructure value, Stream xmlFileStream) { var writer = XmlWriter.Create(xmlFileStream, new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true }); writer.WriteStartDocument(); writer.WriteStartElement(value.Name); WriteStructureContentXml(value, writer); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); }
private void WriteStructureContentXml(RbfStructure value, XmlTextWriter writer) { foreach (var child in value.Children) { if (child is RbfBytes) { var bytesChild = (RbfBytes)child; var contentField = (RbfString)null; foreach (var xyz in value.Children) { if (xyz.Name != null && xyz.Name.Equals("content")) { contentField = (RbfString)xyz; } } if (contentField != null) { if (contentField.Value.Equals("char_array")) { var sb = new StringBuilder(); sb.AppendLine(""); foreach (var k in bytesChild.Value) { sb.AppendLine(k.ToString()); } writer.WriteString(sb.ToString()); } else if (contentField.Value.Equals("short_array")) { var sb = new StringBuilder(); var valueReader = new DataReader(new MemoryStream(bytesChild.Value)); while (valueReader.Position < valueReader.Length) { var y = valueReader.ReadUInt16(); sb.AppendLine(y.ToString()); } writer.WriteString(sb.ToString()); } else { throw new Exception("Unexpected content type"); } } else { string stringValue = Encoding.ASCII.GetString(bytesChild.Value); writer.WriteString(stringValue.Substring(0, stringValue.Length - 1)); } } if (child is RbfFloat) { writer.WriteStartElement(child.Name); var floatChild = (RbfFloat)child; var s1 = string.Format(CultureInfo.InvariantCulture, "{0:0.0###########}", floatChild.Value); writer.WriteAttributeString("value", s1); writer.WriteEndElement(); } if (child is RbfString) { var stringChild = (RbfString)child; if (stringChild.Name.Equals("content")) { writer.WriteAttributeString("content", stringChild.Value); } else if (stringChild.Name.Equals("type")) { writer.WriteAttributeString("type", stringChild.Value); } else { throw new Exception("Unexpected string content"); } } if (child is RbfStructure) { writer.WriteStartElement(child.Name); WriteStructureContentXml((RbfStructure)child, writer); writer.WriteEndElement(); } if (child is RbfUint32) { var intChild = (RbfUint32)child; writer.WriteStartElement(child.Name); writer.WriteAttributeString("value", "0x" + intChild.Value.ToString("X8")); writer.WriteEndElement(); } if (child is RbfBoolean) { var booleanChild = (RbfBoolean)child; writer.WriteStartElement(child.Name); writer.WriteAttributeString("value", booleanChild.Value ? "true" : "false"); writer.WriteEndElement(); } if (child is RbfFloat3) { writer.WriteStartElement(child.Name); var floatVectorChild = (RbfFloat3)child; var s1 = string.Format(CultureInfo.InvariantCulture, "{0:0.0###########}", floatVectorChild.X); var s2 = string.Format(CultureInfo.InvariantCulture, "{0:0.0###########}", floatVectorChild.Y); var s3 = string.Format(CultureInfo.InvariantCulture, "{0:0.0###########}", floatVectorChild.Z); writer.WriteAttributeString("x", s1); writer.WriteAttributeString("y", s2); writer.WriteAttributeString("z", s3); writer.WriteEndElement(); } } }
private static void WriteNode(StringBuilder sb, int indent, RbfStructure rs) { if (rs.Children.Count == 0) { SelfClosingTag(sb, indent, rs.Name); return; } int cind = indent + 1; bool oneline = ((rs.Children.Count == 1) && (rs.Children[0].Name == null)); OpenTag(sb, indent, rs.Name, !oneline); foreach (var child in rs.Children) { if (child is RbfBytes) { var bytesChild = (RbfBytes)child; var contentField = rs.FindChild("content") as RbfString;//TODO: fix this to output nicer XML! if (contentField != null) { OpenTag(sb, cind, "value"); var aind = cind + 1; if (contentField.Value == "char_array") { foreach (byte k in bytesChild.Value) { Indent(sb, aind); sb.AppendLine(k.ToString()); } } else if (contentField.Value.Equals("short_array")) { var valueReader = new DataReader(new MemoryStream(bytesChild.Value)); while (valueReader.Position < valueReader.Length) { Indent(sb, aind); var y = valueReader.ReadUInt16(); sb.AppendLine(y.ToString()); } } else { ErrorXml(sb, aind, "Unexpected content type: " + contentField.Value); } CloseTag(sb, cind, "value"); } else { string stringValue = Encoding.ASCII.GetString(bytesChild.Value); string str = stringValue.Substring(0, stringValue.Length - 1); //removes null terminator sb.Append(str); } } if (child is RbfFloat) { var floatChild = (RbfFloat)child; ValueTag(sb, cind, child.Name, FloatUtil.ToString(floatChild.Value)); } if (child is RbfString) { var stringChild = (RbfString)child; StringTag(sb, cind, stringChild.Name, stringChild.Value); //if (stringChild.Name.Equals("content")) //else if (stringChild.Name.Equals("type")) //else throw new Exception("Unexpected string content"); } if (child is RbfStructure) { WriteNode(sb, cind, child as RbfStructure); } if (child is RbfUint32) { var intChild = (RbfUint32)child; ValueTag(sb, cind, intChild.Name, UintString(intChild.Value)); } if (child is RbfBoolean) { var booleanChild = (RbfBoolean)child; ValueTag(sb, cind, booleanChild.Name, booleanChild.Value.ToString()); } if (child is RbfFloat3) { var v3 = child as RbfFloat3; SelfClosingTag(sb, cind, v3.Name + " x=\"" + FloatUtil.ToString(v3.X) + "\" y=\"" + FloatUtil.ToString(v3.Y) + "\" z=\"" + FloatUtil.ToString(v3.Z) + "\""); } } CloseTag(sb, oneline ? 0 : indent, rs.Name); }