/// <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()); }
/// <summary> /// スニペットXMLを出力する /// </summary> /// <param name="Data">スニペットデータ</param> /// <returns>StringBuilderを返す</returns> public StringWriter MakeSnippetXml(SnippetData Data) { if (Data == null) { return(null); } var sw = new StringWriterUTF8(); using (var w = XmlWriter.Create(sw, Settings)) { // 基本:WriteStartElementとWriteEndElementがセット、タグで囲んだ値はWriteValueを呼んで設定。 // WriteStartAttributeとWriteEndAttributeもセット、ここの値設定はWriteStringを呼ぶ。 // <?xml version="1.0" encoding="utf-8"?> w.WriteStartDocument(); // <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> w.WriteStartElement("CodeSnippets", "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"); // <CodeSnippet Format="1.0.0"> w.WriteStartElement("CodeSnippet"); w.WriteStartAttribute("Format", ""); w.WriteString("1.0.0"); w.WriteEndAttribute(); // Header句 w.WriteStartElement("Header"); // 可変長の要素になる・・・かな? w.WriteStartElement("SnippetTypes"); w.WriteStartElement("SnippetType"); w.WriteValue(Data.SnippetType.ToString()); // 改行せずに値を書く w.WriteEndElement(); w.WriteEndElement(); w.WriteStartElement("Title"); w.WriteValue(Data.Title); w.WriteEndElement(); w.WriteStartElement("Author"); w.WriteValue(Data.Author); w.WriteEndElement(); w.WriteStartElement("Description"); w.WriteValue(Data.Description); w.WriteEndElement(); w.WriteStartElement("HelpUrl"); w.WriteValue(Data.HelpUrl); w.WriteEndElement(); w.WriteStartElement("Shortcut"); w.WriteValue(Data.Shortcut); w.WriteEndElement(); w.WriteEndElement(); // Snippet句 w.WriteStartElement("Snippet"); var isImport = Data.Imports != null && Data.Imports.Count > 0; if (isImport) { //<Imports> // <Import> // <Namespace>System.Data</Namespace> // </Import> // ... //</Imports> w.WriteStartElement("Imports"); foreach (var item in Data.Imports) { w.WriteStartElement("Import"); w.WriteStartElement("Namespace"); w.WriteValue(item); w.WriteEndElement(); w.WriteEndElement(); } w.WriteEndElement(); } var isDeclarations = Data.Declarations != null && Data.Declarations.Count > 0; if (isDeclarations) { //<Declarations> // <Literal> // <ID>type</ID> // <ToolTip>プロパティの型</ToolTip> // <Default>int</Default> // </Literal> // <Literal Editable="false"> // <ID>property</ID> // <ToolTip>プロパティ名</ToolTip> // <Default>MyProperty</Default> // <Function>MyProperty</Function> // </Literal> //</Declarations> w.WriteStartElement("Declarations"); foreach (var item in Data.Declarations) { w.WriteStartElement("Literal"); w.WriteStartElement("ID"); w.WriteValue(item.Id); w.WriteEndElement(); if (item.ToolTip != null) { w.WriteStartElement("ToolTip"); w.WriteValue(item.ToolTip); w.WriteEndElement(); } if (item.Default != null) { w.WriteStartElement("Default"); w.WriteValue(item.Default); w.WriteEndElement(); } if (item.Function != null && item.Function != Function.None) { w.WriteStartElement("Function"); if (item.Editable != null) { w.WriteStartAttribute("Editable", ""); w.WriteString(item.Editable); w.WriteEndAttribute(); } if (!string.IsNullOrWhiteSpace(item.FunctionValue)) { w.WriteValue(item.Function.ToString() + "(" + item.FunctionValue + ")"); } w.WriteEndElement(); } w.WriteEndElement(); } w.WriteEndElement(); } w.WriteStartElement("Code"); w.WriteStartAttribute("Language", ""); w.WriteString(Data.Language.ToString()); w.WriteEndAttribute(); w.WriteStartAttribute("Kind", ""); w.WriteString(Data.Kind.GetStringValue()); w.WriteEndAttribute(); if (!string.IsNullOrWhiteSpace(Data.Delimiter)) { w.WriteStartAttribute("Delimiter", ""); w.WriteString(Data.Delimiter); } w.WriteEndAttribute(); w.WriteCData(Data.Code); // Snippet句ここまで w.WriteEndElement(); w.WriteEndElement(); w.WriteEndElement(); // 完成 w.Flush(); } return(sw); }
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; }