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

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>

Related

Serialize object into XML including "xsi:schemaLocation" and order of attributes

I have this class
public class EnvioDTE
{
[XmlAttribute]
public string version { get; set; }
public EnvioDTE()
{
this.version = "1.0";
}
}
I am serializing it to XML with the following code:
EnvioDTE envioDTE = new EnvioDTE();
string xml = "";
var serializer = new XmlSerializer(typeof(EnvioDTE));
var settings = new XmlWriterSettings
{
Indent = true,
NewLineChars = "\n",
OmitXmlDeclaration = false,
Encoding = Encoding.GetEncoding("ISO-8859-1")
};
using (var stream = new MemoryStream())
{
using (var xmlWriter = XmlWriter.Create(stream, settings))
{
serializer.Serialize(xmlWriter, this);
xml = Encoding.GetEncoding("ISO-8859-1").GetString(stream.ToArray());
}
}
Console.WriteLine(xml);
Which is giving me the following XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<EnvioDTE version="1.0">
</EnvioDTE>
What do I need to add to my code so that I get the following attributes?
<?xml version="1.0" encoding="iso-8859-1"?>
<EnvioDTE xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0">
</EnvioDTE>
The order of the attributes is important for me.
I tried by making two changes:
Change #1
[XmlRoot(Namespace = "http://www.sii.cl/SiiDte")] // *** NEW
public class EnvioDTE
{
[XmlAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] // *** NEW
public string schemaLocation { get; set; } // *** NEW
[XmlAttribute]
public string version { get; set; }
public EnvioDTE()
{
this.version = "1.0";
}
}
Change #2
EnvioDTE envioDTE = new EnvioDTE();
string xml = "";
var namespaces = new XmlSerializerNamespaces(); // *** NEW
namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); // *** NEW
var serializer = new XmlSerializer(typeof(EnvioDTE));
var settings = new XmlWriterSettings
{
Indent = true,
NewLineChars = "\n",
OmitXmlDeclaration = false,
Encoding = Encoding.GetEncoding("ISO-8859-1")
};
using (var stream = new MemoryStream())
{
using (var xmlWriter = XmlWriter.Create(stream, settings))
{
serializer.Serialize(xmlWriter, this, namespaces); // *** NEW
xml = Encoding.GetEncoding("ISO-8859-1").GetString(stream.ToArray());
}
}
Console.WriteLine(xml);
With those changes I'm getting this:
<?xml version="1.0" encoding="iso-8859-1"?>
<EnvioDTE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0" xmlns="http://www.sii.cl/SiiDte">
</EnvioDTE>
And that is not the order of attributes I want...
So you are just trying to read out the XML? I would do this:
XmlReader reader = XmlReader.Create(path, settings);
StringBuilder sb = new StringBuilder();
if (reader != null)
{
while (reader.Read())
{
sb.AppendLine(reader.ReadOuterXml());
}
doc = new XmlDocument();
doc.LoadXml(Convert.ToString(sb));
XmlNodeList xmlNodeList;
xmlNodeList = doc.GetElementsByTagName("Whatever your tag name");
for (i = 0; i < xmlNodeList.Count; i++)
{
if (xmlNodeList[i].InnerXml.Length > 0)
{
foo = xmlNodeList[i].InnerXml;
}
}
}
Okay, you are just trying to output? Then do this. I know it's a bit of a hack and there is probably a million ways to do it:
string myXMLstring = "<?xml version=\"1.0\" encoding="iso-8859-1\"?>" +
<EnvioDTE xmlns=\"http://www.sii.cl/SiiDte\"" +
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
"xsi:schemaLocation=\"http://www.sii.cl/SiiDte EnvioDTE_v10.xsd\"" +
"version=\"1.0\">" +
"</EnvioDTE>";
I've escaped the the quotes and built it up as a string. You can even add \r\n anytime you know you need a new line. I hope that helps. Happy coding!

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

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

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);
}
}
}

XML not written fully

I have a problem with some XML files.
I have about 500+ text files which i read from and extract data and export it to an XML. However, the XML generated is truncated halfway.
<Maps>
<Location>
</Location>
<Locat
It truncates somewhat like that. When i try to open it in notepad, it seems to use up the whole page of notepad. Could it be due to a memory issue since i have quite a large amount of data. Is there any work around on it. I am running the program on a VM
private XmlTextWriter writer = new XmlTextWriter("Maps.xml", System.Text.Encoding.UTF8);
static void Main(string[] args)
{
Program prog = new Program();
String[] filenames = Directory.GetFiles(#"maps_new");
prog.writer.WriteStartDocument();
prog.writer.WriteStartElement("Maps");
foreach (String file in filenames)
{
prog.extractToXML(file);
}
prog.writer.WriteEndElement();
prog.writer.WriteEndDocument();
}
public void extractToXML(String filename)
{
XPathNodeIterator NodeIter;
XPathDocument xmldoc = new XPathDocument(filename);
XPathNavigator nav = xmldoc.CreateNavigator();
String query = "//Schema/#tree";
NodeIter = nav.Select(query);
writer.WriteStartElement("file");
writer.WriteStartAttribute("name");
writer.WriteString(extractFileName(filename));
writer.WriteEndAttribute();
while (NodeIter.MoveNext())
{
writer.WriteStartElement("type");
writer.WriteString(extractFileName(NodeIter.Current.Value.ToString()));
writer.WriteEndElement();
}
NodeIter = nav.Select("//Location");
while (NodeIter.MoveNext())
{
writer.WriteStartElement("Location");
writer.WriteString(NodeIter.Current.Value.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
}
When you use a writer, be sure to Close it when you are done:
try addind this:
//rest of code omitted
foreach (String file in filenames)
{
prog.extractToXML(file);
}
prog.writer.WriteEndElement();
prog.writer.WriteEndDocument();
prog.writer.Flush(); //<-- here
prog.writer.Close(); //<-- and here
and better yet: use a using statement and make extractToXML static:
static void Main(string[] args)
{
using(var writer = new XmlTextWriter("Maps.xml", System.Text.Encoding.UTF8))
{
String[] filenames = Directory.GetFiles(#"maps_new");
writer.WriteStartDocument();
writer.WriteStartElement("Maps");
foreach (String file in filenames)
{
extractToXML(file, writer);
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
public static void extractToXML(String filename, XmlTextWriter wirter)
{
XPathNodeIterator NodeIter;
XPathDocument xmldoc = new XPathDocument(filename);
XPathNavigator nav = xmldoc.CreateNavigator();
String query = "//Schema/#tree";
NodeIter = nav.Select(query);
writer.WriteStartElement("file");
writer.WriteStartAttribute("name");
writer.WriteString(extractFileName(filename));
writer.WriteEndAttribute();
while (NodeIter.MoveNext())
{
writer.WriteStartElement("type");
writer.WriteString(extractFileName(NodeIter.Current.Value.ToString()));
writer.WriteEndElement();
}
NodeIter = nav.Select("//Location");
while (NodeIter.MoveNext())
{
writer.WriteStartElement("Location");
writer.WriteString(NodeIter.Current.Value.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
}

How keep carriage return from parsing XML

I am looking on Internet how keep the carriage return from XML data but I could not find the answer, so I'm here :)
The objective is to write in a file the content of a XML data. So, if the value of the node contains some "\r\n" data, the soft need to write them in file in order to create new line, but it doesn't write, even with space:preserve.
Here is my test class:
XElement xRootNode = new XElement("DOCS");
XElement xData = null;
//XNamespace ns = XNamespace.Xml;
//XAttribute spacePreserve = new XAttribute(ns+"space", "preserve");
//xRootNode.Add(spacePreserve);
xData = new XElement("DOC");
xData.Add(new XAttribute("FILENAME", "c:\\titi\\prout.txt"));
xData.Add(new XAttribute("MODE", "APPEND"));
xData.Add("Hi my name is Baptiste\r\nI'm a lazy boy");
xRootNode.Add(xData);
bool result = Tools.writeToFile(xRootNode.ToString());
And here is my process class:
try
{
XElement xRootNode = XElement.Parse(xmlInputFiles);
String filename = xRootNode.Element(xNodeDoc).Attribute(xAttributeFilename).Value.ToString();
Boolean mode = false;
try
{
mode = xRootNode.Element(xNodeDoc).Attribute(xWriteMode).Value.ToString().ToUpper().Equals(xWriteModeAppend);
}
catch (Exception e)
{
mode = false;
}
String value = xRootNode.Element(xNodeDoc).Value;
StreamWriter destFile = new StreamWriter(filename, mode, System.Text.Encoding.Unicode);
destFile.Write(value);
destFile.Close();
return true;
}
catch (Exception e)
{
return false;
}
Does anybody have an idea?
If you want to preserve cr lf in element or attribute content when saving a XDocument or XElement you can do that by using certain XmlWriterSettings, namely NewLineHandling to Entitize:
string fileName = "XmlOuputTest1.xml";
string attValue = "Line1.\r\nLine2.";
string elementValue = "Line1.\r\nLine2.\r\nLine3.";
XmlWriterSettings xws = new XmlWriterSettings();
xws.NewLineHandling = NewLineHandling.Entitize;
XDocument doc = new XDocument(new XElement("root",
new XAttribute("test", attValue),
elementValue));
using (XmlWriter xw = XmlWriter.Create(fileName, xws))
{
doc.Save(xw);
}
doc = XDocument.Load(fileName);
Console.WriteLine("att value: {0}; element value: {1}.",
attValue == doc.Root.Attribute("test").Value,
elementValue == doc.Root.Value);
In that example the value are preserved in the round trip of saving and loading as the output of the sample is "att value: True; element value: True."
Heres a useful link I found for parsing an Xml string with carraige returns, line feeds in it.
howto-correctly-parse-using-xelementparse-for-strings-that-contain-newline-character-in
It may help those who are parsing an Xml string.
For those who can't be bothered to click it says use an XmlTextReader instead
XmlTextReader xtr = new XmlTextReader(new StringReader(xml));
XElement items = XElement.Load(xtr);
foreach (string desc in items.Elements("Item").Select(i => (string)i.Attribute("Description")))
{
Console.WriteLine("|{0}|", desc);
}

Categories