Reputation: 686
I need to look in a XML document embedded into my VS project(using 2013) and pull out specific elements to use as strings in my application. I have tried going with an XMLReader, XmlDocument and LINQ and for some reason I can't figure out how to pull elements into my code. I haven't found MSDN to be very helpful thus far and I've been googling this for hours. I have never built an app in C# that uses external file, I could really use some guidance on how to approach this? Here is the last thing that I've tried:
XmlDocument doc = new XmlDocument();
doc.Load(@"XML FILEPATH");
// Create an XmlNamespace Manager to resolve the default namespace
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("cfg","urn:config-schema");
XmlNode writeDir;
XmlElement root = doc.DocumentElement;
writeDir = root.SelectSingleNode("descendant::cfg:write_directory",nsmgr);
Console.WriteLine(writeDir.OuterXml);
Console.ReadLine();
My XML looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<settings smlns="urn:config-schema">
<write_directory>"WRITEDIRECTORYLOCATION"</write_directory>
<read_directory>"READDIRECTORYLOCATION"</read_directory>
</settings>
I haven't had much experience with XML's so forgive me if this is sloppy. Thanks everyone!
Upvotes: 0
Views: 106
Reputation: 213
Please provide more information about your problem. Whats wrong with your code right now?
Please consider code below. Its totaly working and print out: "WRITEDIRECTORYLOCATION"
var doc = new XmlDocument();
doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><settings xmlns=\"urn:config-schema\"><write_directory>\"WRITEDIRECTORYLOCATION\"</write_directory><read_directory>\"READDIRECTORYLOCATION\"</read_directory></settings>");
// Create an XmlNamespace Manager to resolve the default namespace
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("cfg", "urn:config-schema");
XmlNode writeDir;
XmlElement root = doc.DocumentElement;
writeDir = root.SelectSingleNode("descendant::cfg:write_directory", nsmgr);
Console.WriteLine(writeDir.InnerText);
Console.ReadLine();
Upvotes: 0
Reputation: 3170
First, as Marc noted in a comment, your namespacedeclaration is wrong: smlns
should be xmlns
.
Second, I have two examples for you of how you could retrieve the data. The first example checks if the elements exists, and then retrieves their value. You could use this example if you are not sure if the elements exist in your file:
XDocument doc = XDocument.Load(@"D:\Temp\file.xml");
if (doc.Root != null)
{
XElement writeDirectoryElement = doc.Root.Elements().FirstOrDefault(e => e.Name.LocalName == "write_directory");
XElement readDirectoryElement = doc.Root.Elements().FirstOrDefault(e => e.Name.LocalName == "read_directory");
string writeDirectory = writeDirectoryElement != null ? writeDirectoryElement.Value : "";
string readDirectory = readDirectoryElement != null ? readDirectoryElement.Value : "";
}
My second example, should be used if you do know that both values exist:
XDocument doc = XDocument.Load(@"D:\Temp\file.xml");
string write_directory = doc.Root.Elements().FirstOrDefault(e => e.Name.LocalName == "write_directory").Value;
string read_directory = doc.Root.Elements().FirstOrDefault(e => e.Name.LocalName == "read_directory").Value;
Upvotes: 1