private static void WritePhp(XmlNode node, Stream stream, Encoding encoding) { IDictionary <string, List <XmlNode> > list; switch (node.NodeType) { case XmlNodeType.Document: list = XDocUtil.CollapseNodeList(node.ChildNodes); stream.Write(encoding, "a:{0}:{{", list.Count); WritePhp(list, stream, encoding); stream.Write(encoding, "}"); break; case XmlNodeType.Element: // check if we have the common case of a simple node (tag and text only without attributes, or tag only without text or attributes) if ((node.Attributes.Count == 0) && (node.ChildNodes.Count == 0)) { Serialize(string.Empty, stream, encoding); stream.Write(encoding, ";"); } else if ((node.Attributes.Count > 0) || (node.ChildNodes.Count != 1) || (node.ChildNodes[0].NodeType != XmlNodeType.Text)) { list = XDocUtil.CollapseNodeList(node.ChildNodes); stream.Write(encoding, "a:{0}:{{", node.Attributes.Count + list.Count); foreach (XmlNode sub in node.Attributes) { WritePhp(sub, stream, encoding); } WritePhp(list, stream, encoding); stream.Write(encoding, "}"); } else { WritePhp(node.ChildNodes[0], stream, encoding); } break; case XmlNodeType.Text: Serialize(node.Value, stream, encoding); stream.Write(encoding, ";"); break; case XmlNodeType.Attribute: Serialize("@" + node.Name, stream, encoding); stream.Write(encoding, ";"); Serialize(node.Value, stream, encoding); stream.Write(encoding, ";"); break; } }
/// <summary> /// Convert an xml document into Php object notation. /// </summary> /// <param name="doc">Document to convert.</param> /// <param name="stream">Stream to write converted document to.</param> /// <param name="encoding">Encoding to use for output.</param> public static void WritePhp(XDoc doc, Stream stream, Encoding encoding) { if (doc.IsEmpty) { stream.Write(encoding, "a:0:{}"); return; } else { List <XmlNode> nodeList = new List <XmlNode>(); foreach (XDoc item in doc) { nodeList.Add(item.AsXmlNode); } IDictionary <string, List <XmlNode> > list = XDocUtil.CollapseNodeList(nodeList); stream.Write(encoding, "a:{0}:{{", list.Count); WritePhp(list, stream, encoding); stream.Write(encoding, "}"); } }