private void AddToDB() { XDocument xdoc; if (!XDocumentExtensions.TryLoad(XmlFileName, out xdoc)) { // File does not exist. Create it. xdoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("LINQ To XML Demo")); } if (xdoc.Root == null) { xdoc.Add(new XElement("Customers")); } xdoc.Root.Add(new XElement("Customer", new XElement("CustId", CustId), new XElement("Name", Name), new XElement("MobileNo", MobileNo), new XElement("Location", Location), new XElement("Address", Address))); try { xdoc.Save(XmlFileName); Console.WriteLine("\n Added \n" + xdoc.ToString()); } catch (Exception ex) { // No documented specific exception types from XDocument.Save() either. Debug.WriteLine(ex); Console.WriteLine(string.Format("Failed to write to XML file {0}", XmlFileName)); } }
public void XDocumentExtensions_RemoveNamespaces_should_not_accept_null_as_parameter() { //Act Action action = () => XDocumentExtensions.RemoveNamespaces(null); //Assert action.Should().Throw <ArgumentNullException>(); }
public void XDocumentExtensions_AsStream_should_not_accept_null_as_parameter() { //Act Action action = () => XDocumentExtensions.AsStream(null); //Assert action.Should().Throw <ArgumentNullException>(); }
public LockResponse Parse(string response, int statusCode, string description) { var xresponse = XDocumentExtensions.TryParse(response); if (xresponse == null || xresponse.Root == null) { return(new LockResponse(statusCode, description)); } var lockdiscovery = xresponse.Root.LocalNameElement("lockdiscovery", StringComparison.OrdinalIgnoreCase); var activeLocks = ParseLockDiscovery(lockdiscovery); return(new LockResponse(statusCode, description, activeLocks)); }
public void Write_DoesntWriteTrackMetadataIfWriteMetadataIsFalse() { MemoryStream stream = new MemoryStream(); using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = false })) { target.Write(_trackWithMetadata); } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_track_single_track_segment)); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null)); }
public void Write_WritesRouteWithMetadata() { MemoryStream stream = new MemoryStream(); using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = true })) { target.Write(_routeWithMetadata); } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_route_with_metadata)); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null)); }
public void Write_WritesWaypointWithoutMetadataIfMetadataIsNull() { MemoryStream stream = new MemoryStream(); using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = false })) { target.Write(_waypoint); } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_waypoint_simple)); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null)); }
public void Constructor_StreamSettings_CreatesGpxFileWithRootElement() { string generatorName = "SpatialLite"; var stream = new MemoryStream(); using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = false, GeneratorName = generatorName })) { } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_empty_file)); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null)); }
public void Write_WritesTrackWithMetadata() { MemoryStream stream = new MemoryStream(); using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = true })) { target.Write(_trackWithMetadata); } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(TestDataReader.Open("gpx-track-with-metadata.gpx")); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected)); }
public void Write_WritesRouteWithoutMetadataIfWriteMetadataIsFalse() { MemoryStream stream = new MemoryStream(); using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = false })) { target.Write(_routeWithMetadata); } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(TestDataReader.Open("gpx-route-single-route.gpx")); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected)); }
public void Constructor_PathSettings_CreatesGpxFileWithRootElement() { string path = PathHelper.GetTempFilePath("gpxwriter-constructor-test-2.gpx"); string generatorName = "SpatialLite"; using (GpxWriter target = new GpxWriter(path, new GpxWriterSettings() { WriteMetadata = false, GeneratorName = generatorName })) { } XDocument written = XDocument.Load(path); XDocument expected = XDocument.Load(TestDataReader.Open("gpx-empty-file.gpx")); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected)); }
public void Write_WritesRouteWithoutUnnecessaryElements() { _routeWithMetadata.Metadata.Source = null; MemoryStream stream = new MemoryStream(); using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = true })) { target.Write(_routeWithMetadata); } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(TestDataReader.Open("gpx-route-with-metadata-selection.gpx")); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected)); }
public void Write_WritesWaypointWithoutUnnecessaryElements() { _waypointWithMetadata.Metadata.SatellitesCount = null; _waypointWithMetadata.Metadata.Name = null; MemoryStream stream = new MemoryStream(); using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = true })) { target.Write(_waypointWithMetadata); } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_waypoint_with_metadata_selection)); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null)); }
public void Constructor_PathSettings_CreatesGpxFileWithRootElement() { string path = "TestFiles\\gpx-writer-constructor-test.gpx"; File.Delete(path); string generatorName = "SpatialLite"; using (GpxWriter target = new GpxWriter(path, new GpxWriterSettings() { WriteMetadata = false, GeneratorName = generatorName })) { } XDocument written = XDocument.Load(path); XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_empty_file)); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null)); }
public void Write_TrackWithEntityDetailsButNullValues_WritesTrackWithoutUnnecessaryElements() { MemoryStream stream = new MemoryStream(); _trackWithMetadata.Metadata.Source = null; using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings() { WriteMetadata = true })) { target.Write(_trackWithMetadata); } XDocument written = XDocument.Load(new MemoryStream(stream.ToArray())); XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_track_with_metadata_selection)); Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null)); }
public ProppatchResponse Parse(string response, int statusCode, string description) { if (string.IsNullOrEmpty(response)) { return(new ProppatchResponse(statusCode, description)); } var xresponse = XDocumentExtensions.TryParse(response); if (xresponse == null || xresponse.Root == null) { return(new ProppatchResponse(statusCode, description)); } var propStatuses = xresponse.Root.LocalNameElements("response", StringComparison.OrdinalIgnoreCase) .SelectMany(MultiStatusParser.GetPropertyStatuses) .ToList(); return(new ProppatchResponse(statusCode, description, propStatuses)); }
public PropfindResponse Parse(string response, int statusCode, string description) { if (string.IsNullOrEmpty(response)) { return(new PropfindResponse(statusCode, description)); } var xresponse = XDocumentExtensions.TryParse(response); if (xresponse == null || xresponse.Root == null) { return(new PropfindResponse(statusCode, description)); } var resources = xresponse.Root.LocalNameElements("response", StringComparison.OrdinalIgnoreCase) .Select(ParseResource) .ToList(); return(new PropfindResponse(statusCode, description, resources)); }
public void WhenIsNull_ThenThrowException() { Assert.Throws <ArgumentNullException>(() => XDocumentExtensions.IsRootName(null, "MyRoot")); }
public override bool Execute() { Log.LogMessage(MessageImportance.Normal, "Analyzing project files for NSwag clients..."); _nswagClients.Clear(); _cachedItems.Clear(); var projectDir = Path.GetDirectoryName(BuildEngine.ProjectFileOfTaskNode) ?? string.Empty; projectDir = projectDir.TrimEnd('\\') + "\\"; var dynamicIncludeTarget = XDocumentExtensions.CreateDynamicIncludeTarget(); using (var cache = new CacheManager(IntermediateOutputPath, Log)) { _cachedItems.Add(new TaskItem(cache.GetCachePath())); var compileItemsToAddGenerator = new List <ITaskItem>(); var compileItemsToRemoveGenerator = new List <ITaskItem>(); foreach (var item in Compile) { Log.LogMessage(MessageImportance.Normal, $"Analyzing '{item.ItemSpec}' compiled item"); var clients = cache.GetCached(item); if (clients == null) { clients = GetClients(item); if (clients.Any() && string.IsNullOrEmpty(item.GetMetadata("Generator"))) { compileItemsToAddGenerator.Add(item); } cache.Cache(item, clients); Log.LogMessage(MessageImportance.Normal, " - Source code analyzed. Results cached."); } else { Log.LogMessage(MessageImportance.Normal, " - Results restored from cache"); } if (!clients.Any()) { if (Equals(item.GetMetadata("Generator"), "MSBuild:UpdateSwaggerClients")) { compileItemsToRemoveGenerator.Add(item); } Log.LogMessage(MessageImportance.Normal, " - There is no NSwag declarations. Skipping."); continue; } foreach (var client in clients) { Log.LogMessage(MessageImportance.Normal, $" - Detected: {client.Namespace}.{client.ClassName} NSwag declaration"); var sourceItemSpec = client.SourceItemSpec; var relativeSourceItemSpec = sourceItemSpec.IsAbsolutePath() ? IOExtensions.MakeRelativePath(projectDir, sourceItemSpec) : sourceItemSpec; var generatedFilePathRelative = Path.Combine(Path.GetDirectoryName(relativeSourceItemSpec), string.Join(".", Path.GetFileNameWithoutExtension(relativeSourceItemSpec), client.ClassName, "cs")); var generatedFilePath = Path.Combine(IntermediateOutputPath, generatedFilePathRelative); Log.LogMessage(MessageImportance.Normal, $" - Shadow source code will be stored in '{generatedFilePath}'"); Path.GetDirectoryName(generatedFilePath).EnsureDirectoryExist(); var taskItem = new TaskItem(generatedFilePath); var relativeGeneratedFilePath = generatedFilePath.IsAbsolutePath() ? IOExtensions.MakeRelativePath(projectDir, generatedFilePath) : generatedFilePath; dynamicIncludeTarget.AddCompileItem(sourceItemSpec, relativeGeneratedFilePath); client.Save(taskItem); taskItem.SetMetadata("Source", item.GetMetadata("FullPath")); _nswagClients.Add(taskItem); } } var dynamicIncludeTargetPath = Path.Combine(IntermediateOutputPath, DynamicIncludeTarget); Path.GetDirectoryName(dynamicIncludeTargetPath).EnsureDirectoryExist(); //Disabled for now //dynamicIncludeTarget.Save(dynamicIncludeTargetPath); _cachedItems.Add(new TaskItem(dynamicIncludeTargetPath)); if (compileItemsToAddGenerator.Any() || compileItemsToRemoveGenerator.Any()) { foreach (var item in compileItemsToAddGenerator) { Log.LogWarning($" - Compile item {item.ItemSpec} contains NSwag client declaration " + "but has no <Generator>MSBuild:UpdateSwaggerClients</Generator> attribute declaration"); } foreach (var item in compileItemsToRemoveGenerator) { Log.LogWarning($" - Compile item {item.ItemSpec} does not contain NSwag client declaration " + "but has <Generator>MSBuild:UpdateSwaggerClients</Generator> attribute declaration"); } } } return(true); }