public void SaveProjectItemsFile(string filePath, DirectoryInfo rootDir = null) { rootDir = rootDir ?? new FileInfo(filePath).Directory; var ns = (XNamespace)"http://schemas.microsoft.com/developer/msbuild/2003"; var doc = new XDocument( new XDeclaration("1.0", "utf-8", null), new XElement(ns + "Project", new XElement(ns + "PropertyGroup", new XElement(ns + "MSBuildAllProjects", "$(MSBuildAllProjects);$(MSBuildThisFileFullPath)"), new XElement(ns + "HasSharedItems", true), new XElement(ns + "SharedGUID", _guid) ), new XElement(ns + "PropertyGroup", new XAttribute("Label", "Configuration"), new XElement(ns + "Import_RootNamespace", _rootNamespace) ), new XElement(ns + "ItemGroup", _compileFiles.Select(a => new XElement(ns + "Compile", new XAttribute("Include", "$(MSBuildThisFileDirectory)" + rootDir.GetRelativePathTo(new FileInfo(a))))).ToArray() ) ) ); var writer = new Utf8StringWriter(); doc.Save(writer); var xml = writer.GetStringBuilder().ToString(); File.WriteAllText(filePath, xml, writer.Encoding); }
private string serializeSoap <T>(T messageInstance) where T : SoapMessage { if (messageInstance == null) { throw new NotSupportedException("Message instance can not be empty."); } SoapMessageAttribute[] array = (SoapMessageAttribute[])messageInstance.GetType().GetCustomAttributes(typeof(SoapMessageAttribute), inherit: false); if (array.Length == 0) { throw new NotSupportedException("SOAP Message must be decorated with SoapMessageAttribute"); } if (string.IsNullOrEmpty(array[0].ElementName) || string.IsNullOrEmpty(array[0].NamespacePrefix) || string.IsNullOrEmpty(array[0].NamespaceUri)) { throw new NotSupportedException("SOAP Message SoapMessageAttribute declaration must specify ElementName, NamespacePrefix and NamespaceUri."); } SoapEnvelope <T> soapEnvelope = new SoapEnvelope <T>(messageInstance); XmlAttributeOverrides overrides = messageOverrides(soapEnvelope.Body.GetType(), messageInstance.GetType(), array[0].ElementName, array[0].NamespaceUri); XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces(); xmlSerializerNamespaces.Add("soap", "http://schemas.xmlsoap.org/soap/envelope/"); xmlSerializerNamespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema"); xmlSerializerNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); xmlSerializerNamespaces.Add(array[0].NamespacePrefix, array[0].NamespaceUri); Utf8StringWriter utf8StringWriter = new Utf8StringWriter(); XmlSerializer xmlSerializer = new XmlSerializer(soapEnvelope.GetType(), overrides); xmlSerializer.Serialize(utf8StringWriter, soapEnvelope, xmlSerializerNamespaces); string result = utf8StringWriter.GetStringBuilder().ToString(); utf8StringWriter.Close(); return(result); }
public string WriteSitemapToString() { using (StringWriter sw = new Utf8StringWriter()) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("image", "http://www.google.com/schemas/sitemap-image/1.1"); ns.Add("video", "http://www.google.com/schemas/sitemap-video/1.1"); XmlSerializer xs = new XmlSerializer(typeof(Sitemap)); xs.Serialize(sw, this, ns); return(sw.GetStringBuilder().ToString()); } }
public void GetPreview() { try { using var sw = new Utf8StringWriter(); ExportXML(sw); Preview = sw.GetStringBuilder().ToString(); } catch (Exception ex) { Preview = "Could not create preview!" + Environment.NewLine + Environment.NewLine + ex.Message; } }
public void SaveSharedProjectFile(string filePath) { var projitems = Path.GetFileNameWithoutExtension(filePath) + ".projitems"; var ns = (XNamespace)"http://schemas.microsoft.com/developer/msbuild/2003"; var doc = new XDocument( new XDeclaration("1.0", "utf-8", null), new XElement(ns + "Project", new XAttribute("ToolsVersion", "14.0"), new XAttribute("DefaultTargets", "Build"), new XElement(ns + "PropertyGroup", new XAttribute("Label", "Globals"), new XElement(ns + "ProjectGuid", _guid), new XElement(ns + "MinimumVisualStudioVersion", "14.0") ), new XElement(ns + "Import", new XAttribute("Project", @"$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"), new XAttribute("Condition", @"Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')") ), new XElement(ns + "Import", new XAttribute("Project", @"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props") ), new XElement(ns + "Import", new XAttribute("Project", @"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props") ), new XElement(ns + "PropertyGroup"), new XElement(ns + "Import", new XAttribute("Project", projitems), new XAttribute("Label", "Shared") ), new XElement(ns + "Import", new XAttribute("Project", @"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets") ) ) ); var writer = new Utf8StringWriter(); doc.Save(writer); var xml = writer.GetStringBuilder().ToString(); File.WriteAllText(filePath, xml, writer.Encoding); }
public ContentResult PickupKml() { var ns = XNamespace.Get("http://www.opengis.net/kml/2.2"); var xmlDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement(ns + "kml", new XElement("Document", new XElement("name", "Untitled layer"), new XElement("Placemark", new XElement("styleUrl", "#poly-000000-1-76"), new XElement("name", "Right Now"), new XElement("description", new XCData("June 6 - June 8")), new XElement("Polygon", new XElement("outerBoundaryIs", new XElement("LinearRing", new XElement("tessellate", 1), new XElement("coordinates", "-85.75172424447373,38.245528009039305,0.0 -85.75184226052443,38.24491290647846,0.0 -85.75260400903062,38.240859826739666,0.0 -85.75266838204698,38.237708211493356,0.0 -85.75060844552354,38.2337979962848,0.0 -85.75095176827745,38.23062921938785,0.0 -85.75181007516221,38.2271231776859,0.0 -85.75078010690049,38.22476324676824,0.0 -85.7511234296544,38.221526645583815,0.0 -85.7522392286046,38.217210953348776,0.0 -85.75284004342393,38.21208573634768,0.0 -85.75181007516221,38.21154621882361,0.0 -85.74786186349229,38.21201829687593,0.0 -85.74623108041123,38.21316475939731,0.0 -85.74288368356065,38.21458095287149,0.0 -85.74099540841416,38.215862247029214,0.0 -85.73601722848252,38.21869450142766,0.0 -85.7331848157628,38.220784904198574,0.0 -85.73052406442002,38.22395410997175,0.0 -85.72812080514268,38.2255049475923,0.0 -85.72468757760362,38.22489810202603,0.0 -85.7196235669835,38.2268534751703,0.0 -85.71541786324815,38.229078491017624,0.0 -85.71104049813584,38.223549538198554,0.0 -85.70820808541612,38.21694121408469,0.0 -85.69490432870225,38.2107369350385,0.0 -85.69378852975206,38.20621826852288,0.0 -85.69155693185166,38.203318081031455,0.0 -85.68614959847764,38.20109227738781,0.0 -85.67954063546495,38.195223923424315,0.0 -85.6646919263585,38.20284594655093,0.0 -85.66640854012803,38.205139142488704,0.0 -85.66864013802842,38.20756715356951,0.0 -85.67413330209092,38.215053011238545,0.0 -85.68511963021592,38.223212391277734,0.0 -85.6936168683751,38.22415639295685,0.0 -85.7042598737462,38.230561795045205,0.0 -85.7089805616124,38.23170796539162,0.0 -85.72443008553819,38.2402025472158,0.0 -85.72116851937608,38.24188786213779,0.0 -85.72340011727647,38.2447191032579,0.0 -85.72726249825791,38.24357313799235,0.0 -85.75172424447373,38.245528009039305,0.0") ) ) ) ), new XElement("Style", new XAttribute("id", "poly-000000-1-76"), new XElement("LineStyle", new XElement("color", "ff000000"), new XElement("width", 1) ), new XElement("PolyStyle", new XElement("color", "4C000000"), new XElement("fill", 1), new XElement("outline", 1) ) ) ) ) ); var wr = new Utf8StringWriter(); xmlDoc.Save(wr); return Content(wr.GetStringBuilder().ToString(), "text/xml"); }
public async Task <HttpResponseMessage> PROPFIND(HttpRequestMessage httpRequestMessage) { string filter = string.Empty; Item entity = null; XElement response = null; XDocument xRequest = null; XDocument xDocument = new XDocument( new XElement(d + "multistatus", new XAttribute(XNamespace.Xmlns + "d", "DAV:") ) ); xDocument.Declaration = new XDeclaration("1.0", "utf-8", "true"); Stream stream = httpRequestMessage.Content.ReadAsStreamAsync().Result; string rowKey = getRowKey(httpRequestMessage.RequestUri); string depth = httpRequestMessage.Headers.Where(h => h.Key.ToLower() == "depth").FirstOrDefault().Value.FirstOrDefault(); string partitionKey = getPartitionKey(httpRequestMessage.RequestUri, false); string rowParent = getPartitionKey(httpRequestMessage.RequestUri, true); if (partitionKey == "wwwdavroot") { filter = "PartitionKey eq 'wwwdavroot'"; if (rowKey != "/") { filter = "PartitionKey eq '" + partitionKey + "' and " + "RowKey eq '" + rowKey + "'"; TableQuery <Item> query = new TableQuery <Item>().Where(filter); IEnumerable <Item> items = itemTable.ExecuteQuery(query); if (items.Count() == 0) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } else { entity = items.First(); response = responseCollection(Host(httpRequestMessage) + httpRequestMessage.RequestUri.LocalPath, entity); } partitionKey = rowKey; } else { response = responseCollection(Host(httpRequestMessage), new Item() { Created = DateTime.Now, RowKey = "", Timestamp = DateTime.Now, Collection = true }); } } else { filter = "PartitionKey eq '" + partitionKey + "' or " + "(RowKey eq '" + rowKey + "' " + "and PartitionKey eq '" + rowParent + "')"; TableQuery <Item> query = new TableQuery <Item>().Where(filter); IEnumerable <Item> items = itemTable.ExecuteQuery(query); if (items.Count() == 0) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } entity = items.First(); response = responseCollection(Host(httpRequestMessage) + httpRequestMessage.RequestUri.LocalPath, entity); } if (stream.Length > 0) { xRequest = XDocument.Load(stream); } if (stream.Length > 0 && xRequest.Descendants(d + "allprop").Count() == 0) { response = new XElement(d + "response", new XElement(d + "href", Host(httpRequestMessage) + httpRequestMessage.RequestUri.LocalPath), new XElement(d + "propstat", new XElement(d + "status", "HTTP/1.1 200 OK"), new XElement(d + "prop") ), new XElement(d + "propstat", new XElement(d + "status", "HTTP/1.1 404 Not Found"), new XElement(d + "prop") ) ); foreach (var xElement in xRequest.Descendants(d + "prop").Descendants()) { filter = "PartitionKey eq '" + entity.BlobName + "' and RowKey eq '" + xElement.Name.NamespaceName + xElement.Name.LocalName + "'"; TableQuery <Prop> query = new TableQuery <Prop>().Where(filter); IEnumerable <Prop> props = propTable.ExecuteQuery(query); if (props.Count() > 0) { response.Descendants(d + "prop").First().Add( new XElement(xElement.Name.NamespaceName + xElement.Name.LocalName, xElement.Value) ); } else { response.Descendants(d + "prop").Last().Add( new XElement(xElement.Name.Namespace + xElement.Name.LocalName) ); } } } xDocument.Element(d + "multistatus").Add(response); if (depth == "1") { filter = "PartitionKey eq '" + partitionKey + "'"; TableQuery <Item> query = new TableQuery <Item>().Where(filter); IEnumerable <Item> items = itemTable.ExecuteQuery(query); if (partitionKey == "wwwdavroot") { partitionKey = ""; } foreach (Item item in items) { if (item.Collection) { response = responseCollection(Host(httpRequestMessage) + item.PartitionKey.Replace("&dir;", "/") + "/" + item.RowKey + "/", item); } else { response = responseCollection(Host(httpRequestMessage) + partitionKey.Replace("&dir;", "/") + "/" + HttpUtility.HtmlEncode(item.RowKey), item); } xDocument.Element(d + "multistatus").Add(response); } } HttpResponseMessage httpResponseMessage = new HttpResponseMessage((HttpStatusCode)207); Utf8StringWriter utf8writer = new Utf8StringWriter(); xDocument.Save(utf8writer); StringBuilder sb = utf8writer.GetStringBuilder(); httpResponseMessage.Content = new StringContent(sb.ToString(), Encoding.UTF8, "application/xml"); return(httpResponseMessage); }
public async Task <HttpResponseMessage> LOCK(HttpRequestMessage httpRequestMessage) { string owner = string.Empty, lockscope = string.Empty, locktype = string.Empty, locktoken = string.Empty; Item item = getItem(httpRequestMessage); string timeout = "Second-3600"; HttpResponseMessage httpResponseMessage = new HttpResponseMessage(); KeyValuePair <string, IEnumerable <string> > timeoutHeader = httpRequestMessage.Headers .Where(h => h.Key == "Timeout").FirstOrDefault(); if (!string.IsNullOrEmpty(timeoutHeader.Value.FirstOrDefault())) { timeout = timeoutHeader.Value.FirstOrDefault(); } Stream stream = httpRequestMessage.Content.ReadAsStreamAsync().Result; if (stream.Length > 0) //New Lock { httpResponseMessage.StatusCode = HttpStatusCode.Created; XDocument xRequestDocument = XDocument.Load(stream); owner = xRequestDocument.Descendants(d + "owner").First().Descendants(d + "href").First().Value; lockscope = xRequestDocument.Descendants(d + "lockscope").First().Descendants().First().Name.LocalName; locktype = xRequestDocument.Descendants(d + "locktype").First().Descendants().First().Name.LocalName; locktoken = "opaquelocktoken:" + Guid.NewGuid().ToString(); } else // Renew existing lock { httpResponseMessage.StatusCode = HttpStatusCode.OK; KeyValuePair <string, IEnumerable <string> > ifHeader = httpRequestMessage.Headers .Where(h => h.Key == "If").FirstOrDefault(); if (!string.IsNullOrEmpty(ifHeader.Value.FirstOrDefault())) { locktoken = ifHeader.Value.First().Substring(2, ifHeader.Value.First().Length - 4); } string filter = "RowKey eq '" + locktoken + "'"; TableQuery <Lock> query = new TableQuery <Lock>().Where(filter); IEnumerable <Lock> locks = lockTable.ExecuteQuery(query); Lock lockItem = locks.First(); lockscope = lockItem.Scope; locktype = lockItem.Type; owner = lockItem.Owner; } Lock itemLock = new Lock() { PartitionKey = item.BlobName, RowKey = locktoken, Expires = DateTime.Now.AddSeconds(int.Parse(timeout.Substring(timeout.IndexOf("-") + 1))), Scope = lockscope, Type = locktype, Owner = owner }; TableOperation insertOperation = TableOperation.InsertOrReplace(itemLock); lockTable.Execute(insertOperation); XDocument xDocument = new XDocument( new XElement(d + "prop", new XElement(d + "lockdiscovery", new XElement(d + "activelock", new XElement(d + "locktype", new XElement(d + locktype) ), new XElement(d + "lockscope", new XElement(d + lockscope) ), new XElement(d + "depth", "infinity" //0 ), new XElement(d + "locktoken", new XElement(d + "href", locktoken) ), new XElement(d + "timeout", timeout), new XElement(d + "owner", owner), new XElement(d + "lockroot", new XElement(d + "href", Host(httpRequestMessage) + httpRequestMessage.RequestUri.LocalPath) ) ) ), new XAttribute(XNamespace.Xmlns + "d", "DAV:") ) ); xDocument.Declaration = new XDeclaration("1.0", "utf-8", "true"); Utf8StringWriter utf8writer = new Utf8StringWriter(); xDocument.Save(utf8writer); StringBuilder sb = utf8writer.GetStringBuilder(); httpResponseMessage.Content = new StringContent(sb.ToString(), Encoding.UTF8, "application/xml"); httpResponseMessage.Content.Headers.Add("Lock-Token", "<" + locktoken + ">"); return(httpResponseMessage); }
public bool Save(string pathToSaveFile, bool overwrite = false) { if (!IsValidPath(pathToSaveFile)) { Debug.LogError("EgoXproject: Invalid Save Path: " + pathToSaveFile); return(false); } if (!overwrite) { if (_path != pathToSaveFile && File.Exists(pathToSaveFile)) { Debug.LogError("EgoXproject: File exists: " + pathToSaveFile); return(false); } } XDocument doc = new XDocument(_decl); bool dtd = false; if (_loaded) { if (_usedDocType != null) { doc.AddFirst(_usedDocType); dtd = true; } } else { doc.AddFirst(_docType); dtd = true; } XElement plist = new XElement("plist"); plist.SetAttributeValue("version", _version); plist.Add(_dict.Xml()); doc.Add(plist); var stringWriter = new Utf8StringWriter(); doc.Save(stringWriter); var content = stringWriter.GetStringBuilder().ToString(); //http://stackoverflow.com/questions/10303315/convert-stringwriter-to-string string[] separator = { System.Environment.NewLine }; string[] lines = content.Split(separator, StringSplitOptions.RemoveEmptyEntries); //Remove the spurious [] that get added to DOCTYPE. Grrr. if (dtd) { for (int ii = 0; ii < lines.Length; ii++) { var line = lines[ii]; if (line.Contains("<!DOCTYPE") && line.Contains("[]")) { lines[ii] = line.Replace("[]", ""); break; } } } File.WriteAllLines(pathToSaveFile, lines); //TODO this is a crap check. bool bSaved = File.Exists(pathToSaveFile); if (bSaved) { _path = pathToSaveFile; } return(bSaved); }