XmlDoc throws the process cannot access the file from tome to time? - c#

I have the following code for writing XML file based on datacontracts
public static void LogDataContractToFile(string XMLStringToLog, string filePathAndName)
{
//String documentPath = string.Empty;
String xmlObject = string.Empty;
FileInfo fileinfo;
XmlDocumentFragment xmlDocumentFragment;
XmlTextWriter xmlWriter;
XmlDocument xmlDocument = null;
lock (LogDataContractToFileLock)
{
filePathAndName = filePathAndName.ToLower();
while (_workingWithFile.Contains(filePathAndName))
Thread.Sleep(1000);
_workingWithFile.Add(filePathAndName.ToLower());
try
{
#region Create XMLFile
fileinfo = new FileInfo(filePathAndName);
if (!fileinfo.Exists)
{
DirectoryInfo info = new DirectoryInfo(fileinfo.DirectoryName);
if (info.Exists == false)
info.Create();
using (xmlWriter = new XmlTextWriter(filePathAndName, System.Text.Encoding.UTF8))
{
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("root");
xmlWriter.WriteStartElement("objects");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
}
else
{
//Se så att filen är 50 MB eller mindre
while (fileinfo.Length > 52428800)
{
xmlDocument = new XmlDocument();
xmlDocument.Load(filePathAndName);
xmlDocument.RemoveChild(xmlDocument.LastChild);
xmlDocument.Save(filePathAndName);
xmlDocument = null;
}
}
#endregion
xmlObject = XMLStringToLog;
//Open document
xmlDocument = new XmlDocument();
xmlDocument.Load(filePathAndName);
//Create a new fragment in current document
xmlDocumentFragment = xmlDocument.CreateDocumentFragment();
xmlDocumentFragment.InnerXml = xmlObject;
//Add new fragment after the first child
xmlDocument.DocumentElement.InsertBefore(xmlDocumentFragment, xmlDocument.DocumentElement.FirstChild);
xmlDocument.Save(filePathAndName);
xmlDocument = null;
}
finally
{
_workingWithFile.Remove(filePathAndName.ToLower());
}
}
}
The problem is that from time to time I get a The process cannot access the file exception? XmlDocument do not have any dispose so I can´t use using. How should this be handled properly?
Note that Im stuck on .NET 4.0.

To safely check for an element and add it if it doesn't exist you should use a ConcurrentDictionary:
private readonly ConcurrentDictionary<string,bool> _workingWithFile = new ConcurrentDictionary<string,bool>();
public static void LogDataContractToFile(string XMLStringToLog, string filePathAndName)
{
...
lock (LogDataContractToFileLock)
{
...
while(!_workingWithFile.TryAdd(filePathAndName, true))
{
Thread.Sleep(1000);
}
...
try
{
...
}
finally
{
//Perhaps check the result here.
bool result;
_workingWithFile.TryRemove(filePathAndName, out result);
}
}
}

Related

XML ReadLine to txt file

I have a problem with saving data from XML URL nodes, using XMLReader, to a text file. Can you please help me out? I don't know how to do it.
Here is the code:
namespace XMLdemo2
{
class Program
{
static void Main(string[] args)
{
// Start with XmlReader object
String URLString = "https://www.shortcut.lv/xmls/tiesraide/ltv1.xml";
XmlTextReader reader = new XmlTextReader(URLString);
{
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name.ToString())
{
case "auth_token":
Console.WriteLine("Tokens IR : " + reader.ReadString());
break;
}
//Console.WriteLine("");
}
}
Console.ReadKey();
}
}
}
}
You can try something easier like this (if it's only one line you want to read)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("https://www.shortcut.lv/xmls/tiesraide/ltv1.xml");
XmlNode authTokenNode = xmlDoc.SelectSingleNode("//auth_token");
if(authTokenNode != null)
Console.WriteLine(authTokenNode.InnerText);
If it is multiple lines
XmlDocument xmlDoc = new XmlDocument();
XmlNodeList itemNodes = xmlDoc.SelectNodes("//auth_token");
foreach(XmlNode itemNode in itemNodes)
{
if((itemNode != null)
Console.WriteLine(itemNode.InnerText);
}

C# append object to xml file using serialization

I am trying append a serialized object to an existing xml file beneath the root element, which I thought would be simple but is proving to be a little challenging.
The problem is in the AddShortcut method but I added some more code for completeness.
I believe what I need to do is:
load the file into an XmlDocument.
navigate to the node I want to append beneath (here the node name is Shortcuts).
create some type of writer and then serialize the object.
save the XmlDocument.
The trouble is in steps 2 and 3. I have tried different variations but I think using XPathNavigator somehow to find the "root" node to append under is a step in the right direction.
I have also looked at almost every question on Stack Overflow on the subject.
Any suggestions welcome. Here is my code
class XmlEngine
{
public string FullPath { get; set; } // the full path to the xmlDocument
private readonly XmlDocument xDoc;
public XmlEngine(string fullPath, string startElement, string[] rElements)
{
FullPath = fullPath;
xDoc = new XmlDocument();
CreateXmlFile(FullPath, startElement, rElements);
}
public void CreateXmlFile(string path, string startElement, string[] rElements)
{
try
{
if (!File.Exists(path))
{
// create a txt writer
XmlTextWriter wtr = new XmlTextWriter(path, System.Text.Encoding.UTF8);
// make sure the file is well formatted
wtr.Formatting = Formatting.Indented;
wtr.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
wtr.WriteStartElement(startElement);
wtr.Close();
// write the top level root elements
writeRootElements(path, rElements);
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.WriteLine("Could not create file: " + path);
}
}
public void AddShortcut(Shortcut s)
{
xDoc.Load(FullPath);
rootNode = xDoc.AppendChild(xDoc.CreateElement("Shortcuts"));
var serializer = new XmlSerializer(s.GetType());
using (var writer = new StreamWriter(FullPath, true))
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.OmitXmlDeclaration = true;
serializer.Serialize(writer, s);
}
xDoc.Save(FullPath);
}
}
This code sample worked for me:
xml:
<?xml version="1.0" encoding="UTF-8"?>
<Launchpad>
<Shortcuts>
<Shortcut Id="1">
<Type>Folder</Type>
<FullPath>C:\SomePath</FullPath>
<Name>SomeFolderName</Name>
</Shortcut>
</Shortcuts>
</Launchpad>
Method:
public void AddShortcut(Shortcut s)
{
xDoc.Load(FullPath);
var rootNode = xDoc.GetElementsByTagName("Shortcuts")[0];
var nav = rootNode.CreateNavigator();
var emptyNamepsaces = new XmlSerializerNamespaces(new[] {
XmlQualifiedName.Empty
});
using (var writer = nav.AppendChild())
{
var serializer = new XmlSerializer(s.GetType());
writer.WriteWhitespace("");
serializer.Serialize(writer, s, emptyNamepsaces);
writer.Close();
}
xDoc.Save(FullPath);
}
load the file into an XmlDocument.
navigate to the node I want to append beneath (here the node name is Shortcuts).
create some type of writer and then serialize the object.
save the XmlDocument
So:
public void AddShortcut(Shortcut s)
{
// 1. load existing xml
xDoc.Load(FullPath);
// 2. create an XML node from object
XmlElement node = SerializeToXmlElement(s);
// 3. append that node to Shortcuts node under XML root
var shortcutsNode = xDoc.CreateElement("Shortcuts")
shortcutsNode.AppendChild(node);
xDoc.DocumentElement.AppendChild(shortcutsNode);
// 4. save changes
xDoc.Save(FullPath);
}
public static XmlElement SerializeToXmlElement(object o)
{
XmlDocument doc = new XmlDocument();
using(XmlWriter writer = doc.CreateNavigator().AppendChild())
{
new XmlSerializer(o.GetType()).Serialize(writer, o);
}
return doc.DocumentElement;
}
This post

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>

save xml file using streamwriter

I am trying to find a way to save data back to an xml file with an encoding "iso-8859-7".
firstly am loading the xml using
public XmlDocument LoadDocument(String x)
{
XmlDocument document = new XmlDocument();
StreamReader stream = new StreamReader(xml, Encoding.GetEncoding("iso-8859-7"));
document.Load(stream);
return (document);
}
to load attributes inside form controls and then when the save button is clicked
private void savebtn_Click(object sender, EventArgs e)
{
XmlNodeList attributes = commonMethods.LoadDocument(xml).DocumentElement.SelectNodes("//Class[#Name='" + classname + "']/Property[#Id='" + id + "']/attribute::*");
for (int x = 0; x < attributes.Count; )
{
foreach (Control ctr in table1.Controls)
{
if (ctr is TextBox)
{
if (ctr.Text == attributes[x].Value.ToString()) { x++; }
else
{
attributes[x].Value = ctr.Text; commonMethods.SaveDocument(xml);
x++;
}
}
else if (ctr is ComboBox)
{
if (((ComboBox)ctr).Text == attributes[x].Value) { x++; }
else
{
attributes[x].Value = ((ComboBox)ctr).Text; commonMethods.SaveDocument(xml);
x++;
}
}
}
}
}
it saves the changes back to the xml file. I used to save it without using xmlwriter like xmldoc.Save("sample.xml) but because of some characters inside the file I had to use a different approach like.
public XmlDocument SaveDocument(String x)
{
XmlDocument document = new XmlDocument();
StreamWriter stream = new StreamWriter(x,false,Encoding.GetEncoding("iso-8859-7"));
document.Save(xml);
return (document);
}
The problem is when I compile it says "xml is used by another process" and it fails.
System.IO.IOException
You've got this exception because the file is still opened by the StreamReader that is pending for finalization (garbage collection).
You should always dispose your streams (and reader / writers) to release the file handle as soon as possible.
public XmlDocument LoadDocument(String path)
{
XmlDocument document = new XmlDocument();
using (StreamReader stream = new StreamReader(path, Encoding.GetEncoding("iso-8859-7")))
{
document.Load(stream);
}
return (document);
}
public XmlDocument SaveDocument(XmlDocument document, String path)
{
using (StreamWriter stream = new StreamWriter(path,false,Encoding.GetEncoding("iso-8859-7")))
{
document.Save(stream);
}
return (document);
}
private void savebtn_Click(object sender, EventArgs e)
{
var doc = commonMethods.LoadDocument(xml);
XmlNodeList attributes = doc.DocumentElement.SelectNodes("//Class[#Name='" + classname + "']/Property[#Id='" + id + "']/attribute::*");
for (int x = 0; x < attributes.Count; )
{
foreach (Control ctr in table1.Controls)
{
if (ctr is TextBox)
{
if (ctr.Text == attributes[x].Value.ToString()) { x++; }
else
{
attributes[x].Value = ctr.Text; commonMethods.SaveDocument(doc, xml);
x++;
}
}
else if (ctr is ComboBox)
{
if (((ComboBox)ctr).Text == attributes[x].Value) { x++; }
else
{
attributes[x].Value = ((ComboBox)ctr).Text; commonMethods.SaveDocument(doc, xml);
x++;
}
}
}
}
}
you didn't dispose the StreamReader object after loading the document
add the using statement:
public XmlDocument LoadDocument(String x)
{
XmlDocument document = new XmlDocument();
using (StreamReader stream = new StreamReader(xml, Encoding.GetEncoding("iso-8859-7")))
{
document.Load(stream);
}
return (document);
}
reference: http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx

Possible to validate xml against xsd using code at runtime?

I have xml files that I read in at runtime, is is possible to validate the xml against an xsd file at runtime? using c#
Try this:
public void ValidateXmlDocument(
XmlReader documentToValidate, string schemaPath)
{
XmlSchema schema;
using (var schemaReader = XmlReader.Create(schemaPath))
{
schema = XmlSchema.Read(schemaReader, ValidationEventHandler);
}
var schemas = new XmlSchemaSet();
schemas.Add(schema);
var settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = schemas;
settings.ValidationFlags =
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += ValidationEventHandler;
using (var validationReader = XmlReader.Create(documentToValidate, settings))
{
while (validationReader.Read())
{
}
}
}
private static void ValidationEventHandler(
object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Error)
{
throw args.Exception;
}
Debug.WriteLine(args.Message);
}
I GOT CODE TOO! I use this in my tests:
public static bool IsValid(XElement element, params string[] schemas)
{
XmlSchemaSet xsd = new XmlSchemaSet();
XmlReader xr = null;
foreach (string s in schemas)
{ // eh, leak 'em.
xr = XmlReader.Create(
new MemoryStream(Encoding.Default.GetBytes(s)));
xsd.Add(null, xr);
}
XDocument doc = new XDocument(element);
var errored = false;
doc.Validate(xsd, (o, e) => errored = true);
if (errored)
return false;
// If this doesn't fail, there's an issue with the XSD.
XNamespace xn = XNamespace.Get(
element.GetDefaultNamespace().NamespaceName);
XElement fail = new XElement(xn + "omgwtflolj/k");
fail.SetAttributeValue("xmlns", xn.NamespaceName);
doc = new XDocument(fail);
var fired = false;
doc.Validate(xsd, (o, e) => fired = true);
return fired;
}
This one takes in the schemas as strings (file resources within the assembly) and adds them to a schema set. I validate and if its not valid I return false.
If the xml isn't found to be invalid, I do a negative check to make sure my schemas aren't screwed up. Its not guaranteed foolproof, but I have used this to find errors in my schemas.
simpler solution..
try
{
XmlReaderSettings Xsettings = new XmlReaderSettings();
Xsettings.Schemas.Add(null, "personDivideSchema.xsd");
Xsettings.ValidationType = ValidationType.Schema;
XmlDocument document = new XmlDocument();
document.Load("person.xml");
XmlReader reader = XmlReader.Create(new StringReader(document.InnerXml), Xsettings);
while (reader.Read());
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}

Categories