/// <summary> /// Serialize an object and return the Xml value /// </summary> /// <param name="obj"></param> /// <returns></returns> public virtual string Serialize <T>(T obj) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer sr = new XmlSerializer(typeof(T)); string output = string.Empty; var writer = new StringWriterUTF8(new StringBuilder()); try { sr.Serialize(writer, obj, ns); output = writer.ToString(); } catch (XmlException xe) { Logger.Error("while deserializing FromXML", xe); } catch (InvalidOperationException ioe) { Logger.Error("while deserializing FromXML", ioe); } catch (Exception ex) { Logger.Error("while deserializing FromXML", ex); throw; } finally { writer.Close(); } return(output); }
/// <summary> /// XML ドキュメントを UTF-8 形式の文字列に変換します。 /// </summary> /// <param name="doc"></param> /// <returns></returns> public static string ToStringXml(this XmlDocument doc) { StringWriterUTF8 writer = new StringWriterUTF8(); doc.Save(writer); string r = writer.ToString(); writer.Close(); return(r); }
public static string XElementToString(XElement xml) { var sw = new StringWriterUTF8(CultureInfo.CurrentCulture); var writer = XmlWriter.Create(sw, new XmlWriterSettings() { Indent = true, IndentChars = "\t", Encoding = Encoding.UTF8 }); xml.WriteTo(writer); writer.Flush(); writer.Close(); return(sw.ToString()); }
/// <summary> /// /// </summary> /// <param name="ParamValue"></param> /// <returns></returns> public static string SerializeParam(object ParamValue) { XmlRootAttribute xRoot = new XmlRootAttribute(); // Use default ElementName but override with our xmlns for getting the type (equiv of xsi:type) xRoot.Namespace = ParamValue.GetType().AssemblyQualifiedName; xRoot.IsNullable = false; XmlSerializer serializer = new XmlSerializer(ParamValue.GetType(), xRoot); TextWriter TWriter = new StringWriterUTF8(); XmlWriter XWriter = new XmlTextWriter(TWriter); // force UTF-8 Encoding // Serialize using the XmlTextWriter. serializer.Serialize(XWriter, ParamValue); XWriter.Close(); return(TWriter.ToString()); }
/// <summary> /// ''' 設定のXMLファイルを取得 /// ''' </summary> /// ''' <returns></returns> /// ''' <remarks></remarks> public static string GetXmlCode() { System.IO.StringWriter SWtr = new StringWriterUTF8(); try { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(Settings)); // シリアル化して書き込む xs.Serialize(SWtr, Instance); SWtr.Close(); } catch (Exception ex) { } finally { } return(SWtr.ToString()); }
private void button2_Click(object sender, EventArgs e) { string username = textBoxUserID.Text; string passcode = textBoxPasscode.Text; XmlDocument document = new XmlDocument(); XmlDeclaration declaration = document.CreateXmlDeclaration("1.0", "UTF-8", null); // XML宣言 XmlElement sync2ch_request = CreateRequest(document, sync_number, client_id); document.AppendChild(declaration); document.AppendChild(sync2ch_request); XmlElement thread_group = document.CreateElement("thread_group"); thread_group.SetAttribute("category", "favorite"); XmlElement dir = document.CreateElement("dir"); dir.SetAttribute("name", "フォルダ1"); thread_group.AppendChild(dir); XmlElement th = CreateThread(document); dir.AppendChild(th); sync2ch_request.AppendChild(thread_group); StringWriterUTF8 writer = new StringWriterUTF8(); document.Save(writer); string xmlString = writer.ToString(); writer.Close(); byte[] postDataBytes = Encoding.UTF8.GetBytes(xmlString); System.Net.HttpWebRequest webreq = (System.Net.HttpWebRequest) System.Net.WebRequest.Create("http://sync2ch.com/api/sync2"); //認証の設定 webreq.Credentials = new System.Net.NetworkCredential(username, passcode); System.Net.ServicePointManager.Expect100Continue = false; webreq.Method = "POST"; webreq.ContentType = "application/x-www-form-urlencoded"; webreq.ContentLength = postDataBytes.Length; //HttpWebResponseの取得 Stream reqStream = webreq.GetRequestStream(); reqStream.Write(postDataBytes, 0, postDataBytes.Length); reqStream.Close(); System.Net.HttpWebResponse webres = (System.Net.HttpWebResponse)webreq.GetResponse(); //受信して表示 System.IO.Stream st = webres.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(st); String result = sr.ReadToEnd(); textBoxXML.Text = result; //閉じる sr.Close(); st.Close(); }
public static string ToStringXml(this XmlDocument doc) { StringWriterUTF8 writer = new StringWriterUTF8(); doc.Save(writer); string r = writer.ToString(); writer.Close(); return r; }