Deleted xml elements are not being removed - c#

In the following snippet the final string "removed" still has the deleted elements. I am converting to byte[] first because the final implementation input will be in that form. I am looping thru the saved nodes and deleting them for element document. What gives? All ideas greatly appreciated.
static void Main(string[] args)
{
var str =
#"
<CustomerCareReport xmlns='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.FireEms' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<OtherInformation>
<Documents xmlns:a='http://schemas.datacontract.org/2004/07/Foo.Bar.Frameworks.Reporting.Stack.Infrastructure.DataObjects.Documents'>
<a:Document>
<a:Contents>Z2dn</a:Contents>
<a:Description>yoyo</a:Description>
<a:Id>1</a:Id>
</a:Document>
<a:Document>
<a:Contents>ZmhmaA0KZmZmDQpmamZqZmpmDQo=</a:Contents>
<a:Description>test 2</a:Description>
<a:Id>2</a:Id>
</a:Document>
</Documents>
</OtherInformation>
</CustomerCareReport>
";
var element = XElement.Parse(str);
XNamespace ns = element.Name.Namespace;
// convert to bytes firstbecause final implementation input is byte array
byte[] bytes = Encoding.ASCII.GetBytes(str);
using (var stream = new MemoryStream(bytes))
{
var deleteMarked = new List<XElement>();
var xelement = XElement.Load(stream);
var docs = xelement
.Descendants(ns + "Documents");
foreach (XElement doc in docs.Descendants())
{
if (doc.Name.LocalName.Equals("Document"))
{
var delDoc = doc;
deleteMarked.Add(delDoc);
}
}
foreach (var deldoc in deleteMarked)
{
foreach (var node in deldoc.Elements())
{
node.Remove();
}
}
if (deleteMarked.Count > 0)
{
stream.Close();
bytes = stream.ToArray();
}
var removed = System.Text.Encoding.Default.GetString(bytes);
Console.WriteLine(removed);
}
}

Related

How to read really big KML File

So I have code that successfully reads in a kml file using XDocumnet, however, as the kml file is now significantly larger, I can't run the program without getting a System out of memory exception. Can anyone help me convert this code to a different reader, so I no longer get this exception
Here's what worked when the kml file was smaller:
Dictionary<string, List<string>> CountyCoordinates = new Dictionary<string, List<string>>();
List<string> locationList = new List<string>();
var doc = XDocument.Load("gadm36_USA.kml");
XNamespace ns = "http://www.opengis.net/kml/2.2";
var result = doc.Root.Descendants(ns + "Placemark");
List<XElement> extendedDatas = doc.Descendants(ns + "ExtendedData").ToList();
foreach (XElement extendedData in extendedDatas)
{
List<XElement> simpleFields = extendedData.Elements(ns + "SimpleField").ToList();
}
foreach (XElement xmlInfo in result)
{
var region = xmlInfo.Element(ns + "ExtendedData").Element(ns + "SchemaData").Value;
List<XElement> simpleFields = xmlInfo.Element(ns + "ExtendedData").Element(ns + "SchemaData").Elements(ns + "SimpleField").ToList();
//var country = region.Element(ns + "SimpleData").Value;
//var state = region.Element(ns + "SimpleData");
//var cityCounty = region.Element(ns + "SimpleData");
//<Polygon><outerBoundaryIs><LinearRing><coordinates>
locationList = xmlInfo.Element(ns + "MultiGeometry").Element(ns + "Polygon").Element(ns + "outerBoundaryIs").Element(ns + "LinearRing").Element(ns + "coordinates").Value.Split(' ').ToList();
region = region.ToLower();
region = Regex.Replace(region, #"[^\w]", string.Empty);
CountyCoordinates.Add(region, locationList);
}
return CountyCoordinates;
Here is an example of the klm file:
<Placemark>
<Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<ExtendedData><SchemaData schemaUrl="#gadm36_USA_2">
<SimpleData name="NAME_0">United States</SimpleData>
<SimpleData name="NAME_1">Arizona</SimpleData>
<SimpleData name="NAME_2">Pima</SimpleData>
</SchemaData></ExtendedData>
<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>-112.539321899414,31.7949981689454 -112.604850769043,31.8155326843262 -112.626457214355,31.8217906951905 -112.635643005371,31.8249397277833 -112.753486633301,31.8611507415771 -112.9423828125,31.9185504913331 -113.081657409668,31.9619903564454 -113.08381652832,31.9624309539794 -113.33349609375,32.0400009155274 -113.333587646484,32.0450096130371 -113.333930969238,32.0983505249024 -113.333992004394,32.1020011901855 -113.33381652832,32.3513298034668 -113.333892822266,32.4205894470215 -113.333686828613,32.5053520202638 -113.14338684082,32.5051498413086 -113.133605957031,32.5052490234376 -112.963966369629,32.504539489746 -112.929733276367,32.5048217773438 -112.872100830078,32.5048294067383 -112.572570800781,32.5058212280275 -112.202919006348,32.507080078125 -112.099632263184,32.5076789855958 -111.793571472168,32.5066299438478 -111.789756774902,32.5066184997559 -111.756599426269,32.506549835205 -111.73974609375,32.5069694519043 -111.721809387207,32.5064697265625 -111.672340393066,32.5063400268555 -111.65495300293,32.5067405700683 -111.587532043457,32.5069808959962 -111.567413330078,32.5069007873536 -111.567436218262,32.5018920898438 -111.471229553223,32.5019416809083 -111.464157104492,32.5018997192383 -111.446762084961,32.5022697448731 -111.262466430664,32.5030517578125 -111.222785949707,32.5027809143066 -111.20539855957,32.5026588439942 -111.154846191406,32.5027503967285 -111.154747009277,32.5114097595215 -111.098213195801,32.5118904113769 -111.063407897949,32.512062072754 -110.950866699219,32.5124397277833 -110.854629516602,32.5119590759278 -110.840507507324,32.5113601684571 -110.840469360352,32.513641357422 -110.756187438965,32.5145797729493 -110.704528808594,32.5144500732423 -110.694198608398,32.5143318176271 -110.684951782227,32.5146789550781 -110.548492431641,32.5139198303223 -110.448432922363,32.5144195556642 -110.448112487793,32.474781036377 -110.447952270508,32.4273910522462 -110.447196960449,32.270149230957 -110.446952819824,32.2551002502443 -110.443702697754,32.2550506591798 -110.44425201416,32.1711921691896 -110.444358825684,32.1657218933105 -110.446006774902,32.0804901123048 -110.448188781738,32.0800704956055 -110.448440551758,32.0668487548828 -110.448196411133,32.05179977417 -110.447708129883,31.9929695129395 -110.446952819824,31.9209003448487 -110.446708679199,31.9053897857667 -110.448127746582,31.7758693695069 -110.448387145996,31.7621917724611 -110.448165893555,31.7462196350099 -110.448455810547,31.7307109832764 -110.534072875977,31.7309474945069 -110.616989135742,31.7306327819824 -110.66438293457,31.7303009033204 -110.669212341309,31.7308082580568 -110.68376159668,31.7305297851564 -110.690216064453,31.7306003570557 -110.704216003418,31.7307624816895 -110.794136047363,31.7308502197266 -110.852279663086,31.7310104370118 -110.851821899414,31.7255306243896 -110.871200561523,31.7257328033448 -110.890579223633,31.7254600524903 -110.955726623535,31.7247200012206 -111.003646850586,31.7247009277344 -111.161399841309,31.7241706848145 -111.161209106445,31.6388511657716 -111.161590576172,31.5507926940918 -111.159545898438,31.5402812957765 -111.16081237793,31.522029876709 -111.263381958008,31.5218391418457 -111.298286437988,31.5216102600098 -111.365417480469,31.5211029052736 -111.364036560059,31.4234199523926 -111.406181335449,31.4369182586671 -111.440193176269,31.4478206634521 -111.448059082031,31.4503402709962 -111.534820556641,31.4781227111816 -111.618415832519,31.5049114227295 -111.687202453613,31.5267524719238 -111.700218200684,31.5308895111085 -111.769149780273,31.5527782440186 -111.952102661133,31.6104793548585 -112.122657775879,31.664270401001 -112.223472595215,31.6960582733155 -112.539321899414,31.7949981689454</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
</Placemark>
Edit so this is what I've done, this successfully does what I want, but it is very computationally expensive and often ends up using 100% of my cpu and crashing the program
public Dictionary<string, List<string>> LoadZipBoundaries()
{
Dictionary<string, List<string>> CountyCoordinates = new Dictionary<string, List<string>>();
List<string> locationList = new List<string>();
string zip = null;
using (XmlReader reader = XmlReader.Create("utah_zcta.kml"))
{
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "SchemaData")
{
XElement el = XNode.ReadFrom(reader) as XElement;
zip = el.Value.Replace("\n\t\t", ",").Split(',')[1];
}
if (reader.Name == "MultiGeometry")
{
XElement coord = XNode.ReadFrom(reader) as XElement;
locationList = coord.Value.Split(' ').ToList();
CountyCoordinates.Add(zip, locationList);
}
}
}
}
return CountyCoordinates;
}

String.Replace not replacing

I'm doing some file clean up in an xml file and trying to use string.Replace to replace certain text blocks but it does not seem to be replacing the text that I am searching on.
My clean up code is follows
private Stream PrepareFile(string path)
{
string data = File.ReadAllText(path);
var newData = data.Replace("<a:FMax xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:FMax>0</a:FMax>")
.Replace("<a:KVy xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:KVy>0</a:KVy>")
.Replace("<a:Td xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:Td>0</a:Td>")
.Replace("<a:VyLim xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:VyLim>0</a:VyLim>");
var newData2 = newData.Replace("<a:VxTableSxI3_2I xmlns:b=\"http://www.w3.org/2001/XMLSchema\" i:type=\"b:string\"/>", "<a:VxTableSxI3_2I>0</a:VxTableSxI3_2I>");
byte[] bytes = Encoding.ASCII.GetBytes(newData2);
return new MemoryStream(bytes);
}
I should be able to write back to the original 'data' variable, but I split the variables out to be able to compare the strings before and after the replace. My xml file contains the following values(copied verbatim)
<a:LongitudinalTracker z:Id="i58">
<Name xmlns="http://schemas.datacontract.org/2004/07/HmsSim.EntityModule.BaseTypes" i:nil="true"/>
<a:FMax xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string"/>
<a:K>2</a:K>
<a:KVy xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string"/>
<a:Td xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string"/>
<a:VyLim xmlns:b="http://www.w3.org/2001/XMLSchema" i:type="b:string"/>
</a:LongitudinalTracker>
And the before and after strings look identical. I'm sure I am missing something silly, but I can't see what it is. Most of the answers to similar questions point out that the original code is not using the return value, but in this case I am definitely using the return value.
As suggested I am posting the code that ended up solving this.
private Stream PrepareFile(string path)
{
string data = File.ReadAllText(path);
var xml = XDocument.Parse(data);
XNamespace ns = "http://schemas.datacontract.org/2004/07/HmsSim.EntityModule.Entities.SimulationEntities.Track";
var longTracker = from item in xml.Descendants(ns + "LongitudinalTracker") select item;
foreach (var xElement in longTracker.Elements())
{
XNamespace nsI = "http://www.w3.org/2001/XMLSchema-instance";
if (xElement.Attribute(nsI + "type") != null)
{
xElement.Attribute(nsI + "type").Remove();
XAttribute attribute = new XAttribute(nsI + "nil", "true");
xElement.Add(attribute);
}
}
var latTracker = from item in xml.Descendants(ns + "LateralTracker") select item;
foreach (var xElement in latTracker.Elements())
{
XNamespace nsI = "http://www.w3.org/2001/XMLSchema-instance";
if (xElement.Attribute(nsI + "type") != null)
{
xElement.Attribute(nsI + "type").Remove();
XAttribute attribute = new XAttribute(nsI + "nil", "true");
xElement.Add(attribute);
}
}
Stream stream = new MemoryStream();
xml.Save(stream);
// Rewind the stream ready to read from it elsewhere
stream.Position = 0;
return stream;
}
This code works and is less brittle than the original code. As always, suggestions are welcome. Thanks to everyone who commented and led me towards this answer, I appreciate it.

How To Get File Directory In C# And show in xml

this is my cod i want with this get all file in directory and end write all in xml file
private void button3_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string folder = appPath;//Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + #"\Archive\";
string filter = "*.*";
string[] files = Directory.GetFiles(folder, filter);
foreach (string item in files)
{
string string1 = item;
string string2 = appPath;
string result = string1.Replace(string2, "");
MessageBox.Show(result);
doc.LoadXml("<item><name>#" + result + " </name></item>");
// Save the document to a file and auto-indent the output.
using (XmlTextWriter writer = new XmlTextWriter("data.xml", null))
{
writer.Formatting = Formatting.Indented;
doc.Save(writer);
writer.Close();
}
}
}
with this code i get my file in directory and remove path
for example C://folder1/folder2/bin/app.exe
to app.exe
its okay but in the end in xml just write one file
XML Result
<?xml version="1.0"?>
<item>
<name>#\WindowsFormsApplication8.vshost.exe.manifest </name>
</item>
Here:
doc.LoadXml("<item><name>#" + result + " </name></item>");
Every time your loop repeats, you're overwriting all of the XML in your XmlDocument.
If you want to use XmlDocument, try this instead, although there are other (Cleaner) ways to output XML.
var doc = new XmlDocument();
var root = doc.AppendChild(doc.CreateElement("Item"));
foreach (var item in files)
{
var name = root.AppendChild(doc.CreateElement("Name"));
name.InnerText = item;
}
var xmlWriterSettings = new XmlWriterSettings { Indent = true };
using (var writer = XmlWriter.Create("data.xml", xmlWriterSettings))
{
doc.Save(writer);
}
Using XmlSerialiser (cleaner C# code than XDocument):
public class Program
{
[XmlType("Item")]
public class Item
{
[XmlElement("Name")]
public string[] Files { get; set; }
}
static string SerialiseToXml<T>(T obj, bool isFormatted = false)
{
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
var stringBuilder = new StringBuilder();
var xmlWriterSettings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = isFormatted };
using (var xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings))
{
var serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(xmlWriter, obj, ns);
return stringBuilder.ToString();
}
}
static void Main(string[] args)
{
string[] files = {"Apple.txt", "Orange.exe", "Pear.docx", "Banana.xml", "Papaya.xls", "Passionfruit.cs"};
var item = new Item {Files = files};
var xml = SerialiseToXml(item, true);
Console.WriteLine(xml);
}
}
You are overwriting your items.
Here's the code that will write a proper xml:
XmlDocument doc = new XmlDocument();
string appPath = Directory.GetCurrentDirectory();
string folder = appPath;
string filter = "*.*";
string[] files = Directory.GetFiles(folder, filter);
using (XmlTextWriter writer = new XmlTextWriter("data.xml", null))
{
writer.WriteStartDocument();
writer.WriteStartElement("Items");
foreach (string item in files)
{
string string1 = item;
string string2 = appPath;
string result = string1.Replace(string2, "");
writer.WriteElementString("Item","", result);
Console.WriteLine(result);
writer.Formatting = Formatting.Indented;
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
doc.Save(writer);
}
And here's the sample xml,
<?xml version="1.0"?>
<Items>
<Item>\ConsoleApplication1.exe</Item>
<Item>\ConsoleApplication1.exe.config</Item>
<Item>\ConsoleApplication1.pdb</Item>
<Item>\ConsoleApplication1.vshost.exe</Item>
<Item>\ConsoleApplication1.vshost.exe.config</Item>
<Item>\ConsoleApplication1.vshost.exe.manifest</Item>
<Item>\data.xml</Item>
</Items>
thanks for best answers.
in my directory too i have 3 folder and There are more files in any folder ,i want any files in folders write in my xml
For Example
<Items>
<Item>\ConsoleApplication1.exe</Item>
<Item>\ConsoleApplication1.exe.config</Item>
<Item>\ConsoleApplication1.pdb</Item>
<Item>\ConsoleApplication1.vshost.exe</Item>
<Item>\ConsoleApplication1.vshost.exe.config</Item>
<Item>..folder1\gold.dll</Item>
<Item>..images\exit.png</Item>

xdocument re writing xml document [duplicate]

I have a method which load XML to a XDocument and modify its elements then save.
But when I reload it. I got this error :
Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it.
I checking the XML and see that the XDocument didn't save the changed but create a duplicate and save.
It save the old one and the new one like this example xml :
<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
<Ungdung>
<Name>HERE City Lens</Name>
<Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
<Path>/Icon/herecitylens.png</Path>
<Version>Unknown</Version>
<Category>HERE</Category>
<Date>Uknown</Date>
</Ungdung>
<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
<Ungdung>
<Name>HERE City Lens</Name>
<Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
<Path>/Icon/herecitylens.png</Path>
<Version>1.0.0.0</Version>
<Category>HERE</Category>
<Date>Uknown</Date>
</Ungdung>
Here the code I used to modify and save XML :
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Open, FileAccess.ReadWrite))
{
//var xdoc = XDocument.Load("APPSDATA.xml");
var xdoc = XDocument.Load(stream, LoadOptions.None);
var listapp = from c in xdoc.Descendants("Ungdung") select c;
foreach (XElement app in listapp)
{
var xElement = app.Element("Name");
if (xElement != null)
progressIndicator.Text = "Checking " + xElement.Value + "...";
var element = app.Element("Id");
if (element != null)
{
var appId = element.Value;
var appVersion = await GetAppsVersion(appId);
app.Element("Version").Value = appVersion.ToString();
}
}
xdoc.Save(stream);
}
How can I solve this problem ?
Looks like you're appending modified document at the end of current file content. That's why you can't parse it later again.
I would split read and write parts into different using statements:
XDocument xdoc;
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Open, FileAccess.Read))
{
xdoc = XDocument.Load(stream, LoadOptions.None);
}
var listapp = from c in xdoc.Descendants("Ungdung") select c;
foreach (XElement app in listapp)
{
var xElement = app.Element("Name");
if (xElement != null)
progressIndicator.Text = "Checking " + xElement.Value + "...";
var element = app.Element("Id");
if (element != null)
{
var appId = element.Value;
var appVersion = await GetAppsVersion(appId);
app.Element("Version").Value = appVersion.ToString();
}
}
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Truncate, FileAccess.Write))
{
xdoc.Save(stream);
}
Setting FileMode.Truncate on second using statement will clear previous file content, what should fix your problem.

LINQ to read XML file and print results

I got the following XML file (Data.xml):
<root>
<sitecollection name="1A">
<site name="1B">
<maingroup name="1C">
<group name="1D"> </group>
</maingroup>
</site>
</sitecollection>
<sitecollection name="2A">
<site name="2B">
<maingroup name="2C">
<group name="2D"> </group>
</maingroup>
</site>
</sitecollection>
</root>
And I need to print all the all the child elements in this format:
1A
1B
1C
1D
2A
2B
2C
2D
I have the following code so far which needs some adjustment. I could also change it completely if there's an easier method. Thanks for your help
class xmlreader
{
public static void Main()
{
// Xdocument to read XML file
XDocument xdoc = XDocument.Load("Data.xml");
var result = new System.Text.StringBuilder();
var lv1s = from lv1 in xdoc.Descendants("sitecollection")
select new
{
sitecollection = lv1.Attribute("name").Value,
maingroup = lv1.Descendants("group")
};
var lv2s = from lv2 in xdoc.Descendants("site")
select new
{
site = lv2.Attribute("name").Value,
sitetittle = lv2.Descendants()
};
var lv3s = from lv3 in xdoc.Descendants("maingroup")
select new
{
maingroup = lv3.Attribute("name").Value,
};
var lv4s = from lv4 in xdoc.Descendants("group")
select new
{
grouppage = lv4.Attribute("name").Value,
};
// Loop to print results
foreach (var lv1 in lv1s)
{
result.AppendLine(lv1.sitecollection);
foreach (var lv2 in lv2s)
{
result.AppendLine(" " + lv2.Attribute("name").Value);
foreach (var lv3 in lv3s)
{
result.AppendLine(" " + lv3.Attribute("name").Value);
foreach (var lv4 in lv4s)
{
result.AppendLine(" " + lv4.Attribute("name").Value);
}
}
}
}
}
}
With such a uniform hierarchy, recursion can do the job with a lot less code:
void PrintNames(StringBuilder result, string indent, XElement el)
{
var attr = el.Attributes("name");
if (attr != null)
{
result.Append(indent);
result.Append(attr.Value);
result.Append(System.Environment.NewLine);
}
indent = indent + " ";
foreach(var child in el.Elements())
{
PrintNames(result, indent, child);
}
}
...
var sb = new StringBuilder();
PrintNames(sb, String.Empty, xdoc.Root);
How about the following, find all elements with a name attribute, then add spaces based on their depth.
var result = new System.Text.StringBuilder();
var namedElements = doc.Descendants().Where(el => el.Attributes("name")!=null);
foreach(var el in namedElements)
{
int depth = el.Ancestors().Count();
for (int i=0;i<depth;i++)
result.Append(" ");
result.Append(el.Attributes("name").Value);
result.Append(System.Environment.NewLine);
}
NOTE: The above is from memory, so please check the syntax!

Categories