/// <summary> /// Formats the XML file. /// </summary> /// <param name="fileXml">The input XML string.</param> /// <param name="settings">The settings for the XML formatting.</param> /// <returns>A formatted XML file.</returns> public string Format(string fileXml, XmlWriterSettings settings = null) { string formattedXml = string.Empty; /* WiFi XML profile is default ASCII encoded. */ Encoding encoding = Encoding.ASCII; if (string.IsNullOrWhiteSpace(fileXml)) { throw new ArgumentNullException(nameof(fileXml)); } XmlDocument xml = new XmlDocument(); xml.LoadXml(fileXml); /* Retrieve the encoding from the XML header, which is used to configure StringWriter. */ XmlDeclaration declaration = xml.ChildNodes.OfType <XmlDeclaration>().FirstOrDefault(); if (!string.IsNullOrEmpty(declaration.Encoding)) { encoding = Encoding.GetEncoding(declaration.Encoding); } if (settings == null) { // Modify these settings to format the XML as desired settings = new XmlWriterSettings { Indent = true, NewLineOnAttributes = true, Encoding = encoding }; } using (StringWriterWithEncoding sw = new StringWriterWithEncoding(encoding)) { using (var textWriter = XmlWriter.Create(sw, settings)) { xml.Save(textWriter); } sw.Flush(); formattedXml = sw.ToString(); } if (string.IsNullOrEmpty(formattedXml)) { throw new NotImplementedException("Unable to format XML file."); } else { return(formattedXml); } }
public static string Serialize <T>(this T obj, Encoding encoding = null) { var t = typeof(T); var ser = GetSerializer <T>(t); using (var stream = new StringWriterWithEncoding(encoding)) { ser.Serialize(stream, obj); stream.Flush(); return(stream.ToString()); } }
private async Task PerformAuth(HttpClient client) { var jwtToken = await GeJwtTokenAsync(client).ConfigureAwait(false); using (var writer = new StringWriterWithEncoding(Encoding.UTF8)) using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Encoding = Encoding.UTF8 })) { var bytes = Encoding.UTF8.GetBytes(jwtToken); xmlWriter.WriteStartElement( "wsse", "BinarySecurityToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); xmlWriter.WriteAttributeString("ValueType", null, "urn:ietf:params:oauth:token-type:jwt"); xmlWriter.WriteAttributeString( "EncodingType", null, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"); xmlWriter.WriteBase64(bytes, 0, bytes.Length); xmlWriter.WriteEndElement(); xmlWriter.Flush(); writer.Flush(); var token = Uri.EscapeDataString(writer.ToString()); using (var response = await client.PostAsync( "/", new FormUrlEncodedContent( new[] { new KeyValuePair <string, string>("wresult", token), new KeyValuePair <string, string>("wa", "wsignin1.0"), new KeyValuePair <string, string>("wctx", "rm=0&id=passive&ru=%2f") })) .ConfigureAwait(false)) { response.StatusCode.Should().Be(HttpStatusCode.Redirect, "(Post-login) Did not like our generated auth-cookie "); response.Headers.Location.Should().Be("/", "(Post-login) Did not receive original url root redirect"); } using (var response = await client.GetAsync(config.RelativeLoginUrl).ConfigureAwait(false)) response.EnsureSuccessStatusCode(); } }
public static string ToXml(this XDocument document) { Encoding encoding; try { encoding = Encoding.GetEncoding(document.Declaration.Encoding); } catch { encoding = Encoding.UTF8; } using (var writer = new StringWriterWithEncoding(encoding)) { document.Save(writer); writer.Flush(); return(writer.ToString()); } }
private string BuildXml(Node e, bool indent) { if (e != null) { var w = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = indent, IndentChars = " " }; using (var sr = new StringWriterWithEncoding(Encoding.UTF8)) { using (var xw = XmlWriter.Create(sr, w)) WriteTree(this, xw, null); sr.Flush(); return(sr.ToString()); } } return(""); }
public string GetBindings() { checkCookieAndLogin(); try { using (var unit = GetUnitOfWork()) { XElement root = new XElement("root", from wtpb in unit.Scope.Repository <WebToPrintBinding>().GetAll(c => c.WebToPrintBindingFields.Where <WebToPrintBindingField>(field => field.Type > 1).Count() > 0).ToList() // leave out bindings where there are unknowns in the field types select new XElement("Binding", new XAttribute("ID", wtpb.BindingID), new XAttribute("Name", wtpb.Name), new XElement("Inputs", from field in wtpb.WebToPrintBindingFields // inputs are whole numbers where field.Type % 2 == 0 select new XElement("Field", new XAttribute("Name", field.Name), new XAttribute("Type", Enum.GetName(typeof(BindingFieldType), field.Type)), new XAttribute("Options", field.Options))), new XElement("Outputs", from field in wtpb.WebToPrintBindingFields where field.Type % 2 == 1 select new XElement("Field", new XAttribute("Name", field.Name.Replace("[", "").Replace("]", "")), new XAttribute("Type", Enum.GetName(typeof(BindingFieldType), field.Type - 1)))))); root.Add(new XElement("SpecialBinding", new XAttribute("ID", -1), new XAttribute("Name", "Page"), new XElement("Outputs", new XElement("Field", new XAttribute("Name", "Page Number"), new XAttribute("Type", Enum.GetName(typeof(BindingFieldType), BindingFieldType.Int))), new XElement("Field", new XAttribute("Name", "Index"), new XAttribute("Type", Enum.GetName(typeof(BindingFieldType), BindingFieldType.String)))))); root.Add(new XElement("SpecialBinding", new XAttribute("ID", -2), new XAttribute("Name", "Static Image"), new XElement("Inputs", new XElement("Field", new XAttribute("Name", "ImagePath"), new XAttribute("Type", Enum.GetName(typeof(BindingFieldType), BindingFieldType.String)), new XAttribute("Options", (int)BindingFieldOptions.StaticImageSearch))), // third bit set = static image search, see WebToPrintBindingField.cs new XElement("Outputs", new XElement("Field", new XAttribute("Name", "ImageURL"), new XAttribute("Type", Enum.GetName(typeof(BindingFieldType), BindingFieldType.ImageURL)))))); root.Add(new XElement("SpecialBinding", new XAttribute("ID", -3), new XAttribute("Name", "Static Text"), new XElement("Inputs", new XElement("Field", new XAttribute("Name", "TextInput"), new XAttribute("Type", Enum.GetName(typeof(BindingFieldType), BindingFieldType.String)), new XAttribute("Options", (int)BindingFieldOptions.StaticTextInput))), // fourth bit set = static text input new XElement("Outputs", new XElement("Field", new XAttribute("Name", "TextOutput"), new XAttribute("Type", Enum.GetName(typeof(BindingFieldType), BindingFieldType.String)))))); using (var stringWriter = new StringWriterWithEncoding(Encoding.UTF8)) { using (var writer = new XmlTextWriter(stringWriter) { Formatting = Formatting.None }) { writer.WriteStartDocument(true); root.WriteTo(writer); writer.Flush(); writer.WriteEndDocument(); writer.Flush(); } stringWriter.Flush(); return(stringWriter.ToString()); } } } catch (Exception e) { return(""); } }