Ejemplo n.º 1
0
        public static XDocument Serialize <T>(T value, bool useDataContract = false)
        {
            XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", "no"));

            if (!useDataContract)
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (var writer = doc.CreateWriter())
                    xmlSerializer.Serialize(writer, value);
            }
            else
            {
                DataContractSerializer xmlSerializer = new DataContractSerializer(typeof(T));

                using (var writer = doc.CreateWriter())
                    xmlSerializer.WriteObject(writer, value);
            }

            foreach (XElement XE in doc.Root.DescendantsAndSelf())
            {
                // Stripping the namespace by setting the name of the element to it's localname only
                XE.Name = XE.Name.LocalName;
                // replacing all attributes with attributes that are not namespaces and their names are set to only the localname
                XE.ReplaceAttributes((from xattrib in XE.Attributes().Where(xa => !xa.IsNamespaceDeclaration) select new XAttribute(xattrib.Name.LocalName, xattrib.Value)));
            }

            return(doc);
        }
        private static void StripNamespacesFromXml(XDocument xml)
        {
            foreach (XElement XE in xml.Root.DescendantsAndSelf())
            {
                // Stripping the namespace by setting the name of the element to it's localname only

                if (XE.Name.Namespace.NamespaceName.Equals("http://schemas.microsoft.com/msus/2002/12/BaseApplicabilityRules"))
                {
                    XE.Name = $"b.{XE.Name.LocalName}";
                }
                else if (XE.Name.Namespace.NamespaceName.Equals("http://schemas.microsoft.com/msus/2002/12/MsiApplicabilityRules"))
                {
                    XE.Name = $"m.{XE.Name.LocalName}";
                }
                else if (XE.Name.Namespace.NamespaceName.Equals("http://schemas.microsoft.com/msus/2002/12/UpdateHandlers/WindowsDriver"))
                {
                    XE.Name = $"d.{XE.Name.LocalName}";
                }
                else
                {
                    XE.Name = XE.Name.LocalName;
                }
                // replacing all attributes with attributes that are not namespaces and their names are set to only the localname
                XE.ReplaceAttributes((from xattrib in XE.Attributes().Where(xa => !xa.IsNamespaceDeclaration) select new XAttribute(xattrib.Name.LocalName, xattrib.Value)));
            }
        }
Ejemplo n.º 3
0
        public static string Convert(string xml)
        {
            var doc = XDocument.Parse(xml);

            doc.Declaration = null;
            foreach (XElement XE in doc.Root.DescendantsAndSelf())
            {
                XE.Name = XE.Name.LocalName;
                XE.ReplaceAttributes((from xattrib in XE.Attributes().Where(xa => !xa.IsNamespaceDeclaration) select new XAttribute(xattrib.Name.LocalName, xattrib.Value)));
            }
            var xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(doc.ToString());
            var json = JsonConvert.SerializeXmlNode(xmlDocument);

            return(Regex.Replace(json, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase));
        }