Create Attribute, Text Element, Image Element,Read Image Element : Attribute « XML « C# / C Sharp

Create Attribute, Text Element, Image Element,Read Image Element


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public class XmlUtility
    public static XmlAttribute CreateAttribute(XmlNode node, string name, object value)
        XmlAttribute attribute = node.OwnerDocument.CreateAttribute(name);
        attribute.Value = value.ToString();
        return attribute;

    public static XmlElement CreateTextElement(XmlNode node, string name, string value)
        StringBuilder sb = new StringBuilder();
        using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb)))
        XmlElement element = node.OwnerDocument.CreateElement(name, node.NamespaceURI);
        element.InnerText = sb.ToString();
        return element;

    public static XmlElement CreateImageElement(XmlNode node, string name, Image image)
        XmlElement imageElement;
        using (var stream = new MemoryStream())
            image.Save(stream, ImageFormat.Png);
            byte[] imageBytes = stream.ToArray();
            String imageBase64String = Convert.ToBase64String(imageBytes);
            imageElement = CreateTextElement(node, name, imageBase64String);
        return imageElement;

    public static Image ReadImageElement(XmlElement node)
        Image image;
        byte[] imageBytes = Convert.FromBase64String(node.InnerText);
        using (var stream = new MemoryStream(imageBytes))
            image = Bitmap.FromStream(stream);
            return image;



Related examples in the same category

1.Set Attribute Value
2.Get Attribute Value
3.Get Int value from xml attribute
4.Get Attribute value (2)
5.Get attribute or return default value
6.Get Bool Attribute Or Throw exception
7.Get attribute value and convert to integer type
8.Get attribute value and convert to integer type or throw exception
9.Get Attribute and return Int64
10.Get Attribute and return Int64 or throw exception
11.Adds an XmlAttribute to a XmlNode
12.Get boolean value if an attribute is 1 or true
13.Attempts to get and cast attribute from an XmlElement
14.Attempts to get and convert an attribute from an XmlElement
15.Gets a string from an attribute or node.
16.Remove all the xml namespaces (xmlns) attributes in the xml string
17.Get Attribute With Conversion
18.Find a single Attribute of the type 'attributeType' from the supplied class
19.Create Xml Attribute, Node
20.Attribute Util
21.Find and get Attribute value
22.Finds an attribute with the given name in the given XML node and returns the attribute of the value.
23.Parse Attribute
24.Get attribute value (3)
25.Get int type attribute value
26.Get attribute value or throw exception
27.Read an XML file from the xml element and the xml attribute
28.Create and set attribute
29.Adds a new attribute to the given target element.
30.Get Text Attribute
31.Get Int Attribute