示例#1
0
        /// <summary>
        /// construit chaque XElement de la conversion
        /// </summary>
        /// <param name="oXmlNode"></param>
        /// <param name="Namespace"></param>
        /// <param name="indent"></param>
        /// <returns></returns>
        private string BuildElement(System.Xml.XmlNode oXmlNode, string indent)
        {
            string sResult = string.Empty;

            switch (oXmlNode.NodeType)
            {
            case System.Xml.XmlNodeType.Comment:
                break;

            case System.Xml.XmlNodeType.Document:
                break;

            case System.Xml.XmlNodeType.Element:

                sResult += indent + "new " + LinqNamespace + "XElement(";

                if (oXmlNode.ChildNodes.Count == 1 && oXmlNode.FirstChild.NodeType == System.Xml.XmlNodeType.Text)
                {
                    if (!string.IsNullOrEmpty(oXmlNode.NamespaceURI))
                    {
                        sResult += Namespaces[oXmlNode.NamespaceURI] + "+";
                        if (!string.IsNullOrEmpty(oXmlNode.Prefix))
                        {
                            sResult += "\"" + oXmlNode.LocalName + "\",\"" + oXmlNode.InnerText + "\"";
                        }
                        else
                        {
                            sResult += "\"" + oXmlNode.Name + "\",\"" + oXmlNode.InnerText + "\"";
                        }
                    }
                    else
                    {
                        sResult += "\"" + oXmlNode.Name + "\",\"" + oXmlNode.InnerText + "\"";
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(oXmlNode.NamespaceURI))
                    {
                        sResult += Namespaces[oXmlNode.NamespaceURI] + "+";
                        if (!string.IsNullOrEmpty(oXmlNode.Prefix))
                        {
                            sResult += "\"" + oXmlNode.LocalName + "\"";
                        }
                        else
                        {
                            sResult += "\"" + oXmlNode.Name + "\"";
                        }
                    }
                    else
                    {
                        sResult += "\"" + oXmlNode.Name + "\"";
                    }
                }

                if (oXmlNode.Attributes.Count > 0)
                {
                    foreach (System.Xml.XmlAttribute oXmlAttribute in oXmlNode.Attributes)
                    {
                        if (string.IsNullOrEmpty(oXmlAttribute.NamespaceURI))
                        {
                            // attribut
                            if (oXmlAttribute.LocalName != "xmlns")
                            {
                                sResult += ",\r\t" + indent + "new " + LinqNamespace + "XAttribute(\"" + oXmlAttribute.Name + "\",\"" + oXmlAttribute.Value + "\")";
                            }
                        }
                        else
                        {
                            if (oXmlAttribute.NamespaceURI != currentNamespacePrefix)
                            {
                                // LinqNamespace
                                if (!string.IsNullOrEmpty(oXmlNode.Prefix))
                                {
                                    if (oXmlAttribute.Value != oXmlNode.GetNamespaceOfPrefix(oXmlNode.Prefix))
                                    {
                                        sResult += ",\r\t" + indent + "new " + LinqNamespace + "XAttribute(System.Xml.Linq.XNamespace.Xmlns + \"" + oXmlAttribute.LocalName + "\", \"" + oXmlAttribute.Value + "\")";
                                    }
                                }
                                else
                                {
                                    if (oXmlAttribute.LocalName != "xmlns")
                                    {
                                        sResult += ",\r\t" + indent + "new " + LinqNamespace + "XAttribute(System.Xml.Linq.XNamespace.Xmlns + \"" + oXmlAttribute.LocalName + "\", \"" + oXmlAttribute.Value + "\")";
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            }

            if (!string.IsNullOrEmpty(oXmlNode.Prefix))
            {
                if (oXmlNode.GetNamespaceOfPrefix(oXmlNode.Prefix) != currentNamespacePrefix)
                {
                    sResult += ",\r\t" + indent + "new " + LinqNamespace + "XAttribute(System.Xml.Linq.XNamespace.Xmlns + \"" + oXmlNode.Prefix + "\", " + Namespaces[oXmlNode.GetNamespaceOfPrefix(oXmlNode.Prefix)] + ")";
                    currentNamespacePrefix = oXmlNode.GetNamespaceOfPrefix(oXmlNode.Prefix);
                }
            }
            foreach (System.Xml.XmlNode ChildElement in oXmlNode.ChildNodes)
            {
                if (Options.IncludeXComment)
                {
                    if (ChildElement.NodeType == System.Xml.XmlNodeType.Comment)
                    {
                        sResult += ",\r\t" + indent + "new " + LinqNamespace + "XComment(\"" + ChildElement.Value + "\")";
                    }
                }
                if (ChildElement.NodeType == System.Xml.XmlNodeType.Element)
                {
                    sResult += "," + Environment.NewLine;
                    sResult += BuildElement(ChildElement, indent + "\t");
                    sResult += "\r" + indent + "\t)";
                }
            }
            return(sResult);
        }