lucky80
lucky80
Reputation Top 1%
Fabio Luchetta
8 Snippets  (103rd place)
Published
0 Channels
Created
2 Channels
Following
192 points  (159th place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Serious Publisher
Junior Popular Coder
Serious Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Junior Famous Coder
Serious Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by lucky80

public by lucky80  3735  1  5  0

Reindent XML string in C#

How to reindent XML string
public static String GetReindentXMLString(String inputXML)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(inputXML);
            var sb = new StringBuilder();
            var settings =
                new XmlWriterSettings
                {
                    Indent = true,
                    IndentChars = @"    ",
                    NewLineChars = Environment.NewLine,
                    NewLineHandling = NewLineHandling.Replace,
                    NewLineOnAttributes = true,
                    //ConformanceLevel = ConformanceLevel.Fragment,
                    OmitXmlDeclaration = true,
                };

            using (var writer = XmlWriter.Create(sb, settings))
            {
                if (doc.ChildNodes[0] is XmlProcessingInstruction)
                {
                    doc.RemoveChild(doc.ChildNodes[0]);
                }

                doc.Save(writer);
                return sb.ToString();
            }
        }
;

public by lucky80  3607  2  5  0

C#: Serialize to XML from generic object

This function returns the serialized XML as a string given a generic object in input
public static String SerializeToXML<T>(T t, XmlSerializerNamespaces inNameSpaces)
        {
            XmlSerializerNamespaces ns = inNameSpaces;
            if (ns == null)
            {
                ns = new XmlSerializerNamespaces();
                ns.Add("", "");
            }
            XmlSerializer serializer = new XmlSerializer(t.GetType());
            TextWriter textWriter = (TextWriter)new StringWriter();
            serializer.Serialize(textWriter, t, ns);
            return textWriter.ToString();
        }
;

public by lucky80  2875  2  5  0

C#: Deserialize XML generic

This function deserializes the XML string passed in
public static T DeserializeFromXML<T>(ref String inString)
        {
            T retVal;
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
            TextReader textReader = (TextReader)new StringReader(inString);
            retVal = (T)deserializer.Deserialize(textReader);
            textReader.Close();
            return retVal;
        }
;

public by lucky80  2312  8  5  0

C#: Get attributes values from element found on Xpath

public static String[] getAttributesFromElementByXPath(XmlDocument XmlDoc, String[] attName, String Xpath)
       {
           try
           {
               if (attName.Length > 0)
               {
                   String[] strArr_retValues = new String[attName.Length];
                   XmlNode node = XmlDoc.SelectSingleNode(Xpath);
                   for (int i = 0; i < attName.Length; i++)
                   {
						    if (node != null)
							 {
								try
								{
									strArr_retValues[i] = node.Attributes[attName].Value;
								}
								catch (Exception)
								{

									// throw new XmlException("Attribute: " + attName + "not found in node: " + node.Name);                   
								}
							 }
						    else							
							   strArr_retValues[i] = null;
                   }
                   return strArr_retValues;
               }
               return null;

           }
           catch (Exception)
           {

               //throw;
               return null;
           }
       }
;

public by lucky80  2619  1  5  0

C#: Get array string attributes of Enum object

public static string[] GetEnumDescriptionArray<T>()
        {
            Array enumValue = System.Enum.GetValues(typeof(T));
            string[] result = new string[enumValue.Length];
            Int32 count = 0;
            foreach (var item in enumValue)
            {
                FieldInfo fi = item.GetType().GetField(item.ToString());

                DescriptionAttribute[] attributes =
                    (DescriptionAttribute[])fi.GetCustomAttributes(
                        typeof(DescriptionAttribute),
                        false
                );

                if (attributes != null &&
                    attributes.Length > 0)
                {
                    result[count] = attributes[0].Description;
                }
                else
                {
                    //return new string[0];
                    return null;
                }
                count++;
            }
            return result;
        }
;