Reputation: 2055
I am trying to read from an XML file that is supplied through a file browser but the values are alwas null even though I can see the xml file come through to elements.
public void UploadXml(Stream fileStream)
{
//Load xml
fileStream.Position = 0;
var xdoc = XElement.Load(fileStream);
IEnumerable<XElement> elements = xdoc.Elements();
var codeList = new CodeList();
foreach (var item in elements)
{
codeList.Name = item.Element("CODELIST_NAME").Value;
codeList.Description = item.Element("DESRIPTION").Value;
codeList.Version = item.Element("VERSION").Value;
codeList.EffectiveDate = DateTime.Parse(item.Element("EFFECTIVE_DATE").Value);
codeList.ExpirationDate = DateTime.Parse(item.Element("EXPIRATION_DATE").Value);
}
// save code list
// get code list ID
// create codes
}
UPDATE XML
<?xml version="1.0" encoding="utf-8"?>
<CONTEXT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2000/xmlns">
<CONTEXT_NAME></CONTEXT_NAME>
<CODELIST>
<CODELIST_NAME></CODELIST_NAME>
<DESRIPTION></DESRIPTION>
<VERSION></VERSION>
<USERNAME>test user</USERNAME>
<OWNER_TEAM />
<STEWARD_TEAM />
<STATUS></STATUS>
<LAST_MODIFIED></LAST_MODIFIED>
<LAST_MODIFIED_NAME></LAST_MODIFIED_NAME>
<EFFECTIVE_DATE></EFFECTIVE_DATE>
<EXPIRATION_DATE></EXPIRATION_DATE>
<FILE_TIMESTAMP></FILE_TIMESTAMP>
<CONSTRAINED_VALUE>
<CODE></CODE>
<PARENT_ID />
<NAME></NAME>
<DESCRIPTION></DESCRIPTION>
</CONSTRAINED_VALUE>
</CODELIST>
</CONTEXT>
In a normal file there would be data in the file obviously I just had to remove it for the sample.
The constrained value tags will be repeated for rows in a database
Upvotes: 0
Views: 1379
Reputation: 1787
I know you found an answer but i wanted to put what i did to get it to function properly just for fun :) here is the code that i used:
using System;
using System.Xml;
using System.Xml.Linq;
namespace xmlTesting
{
public class codeList
{
public string Name { get; set; }
public string Description { get; set; }
public string Version { get; set; }
public DateTime EffectiveDate { get; set; }
public DateTime ExpirationDate { get; set; }
}
class program {
static void Main(string[] args)
{
var CL = UploadXml(XElement.Load(@"c:\debug\xmlcontent.xml"));
Console.WriteLine(
string.Format("name: {0}\nDesc: {1}\nVersion: {2}\nEffectivdate: {3}\nExp: {4}"
, CL.Name, CL.Description, CL.Version, CL.EffectiveDate, CL.ExpirationDate)
);
Console.ReadKey(true);
}
public static codeList UploadXml(XElement xdoc)
{
var codeList = new codeList();
foreach (XElement XE in xdoc.Descendants())
{
switch (XE.Name.LocalName)
{
case "CODELIST_NAME":
codeList.Name = XE.Value;
break;
case "DESCRIPTION":
if(codeList.Description == null)
codeList.Description = XE.Value;
break;
case "VERSION":
codeList.Version = XE.Value;
break;
case "EFFECTIVE_DATE":
codeList.EffectiveDate = DateTime.Parse(XE.Value);
break;
case "EXPIRATION_DATE":
codeList.ExpirationDate = DateTime.Parse( XE.Value);
break;
}
}
// save code list
// get code list ID
// create codes
return codeList;
}
}
}
and the xml that i used was this.
<?xml version="1.0" encoding="utf-8"?>
<CONTEXT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/2000/xmlns">
<CONTEXT_NAME></CONTEXT_NAME>
<CODELIST>
<CODELIST_NAME>a name</CODELIST_NAME>
<DESCRIPTION>a desr</DESCRIPTION>
<VERSION>test1.1</VERSION>
<USERNAME>test user</USERNAME>
<OWNER_TEAM />
<STEWARD_TEAM />
<STATUS></STATUS>
<LAST_MODIFIED>a day</LAST_MODIFIED>
<LAST_MODIFIED_NAME>trae</LAST_MODIFIED_NAME>
<EFFECTIVE_DATE>07/05/1983</EFFECTIVE_DATE>
<EXPIRATION_DATE>07/05/1983</EXPIRATION_DATE>
<FILE_TIMESTAMP></FILE_TIMESTAMP>
<CONSTRAINED_VALUE>
<CODE></CODE>
<PARENT_ID />
<NAME></NAME>
<DESCRIPTION></DESCRIPTION>
</CONSTRAINED_VALUE>
</CODELIST>
</CONTEXT>
Upvotes: 1
Reputation: 2055
Thanks for the help, it was useful for me to fix this
My problem was with how I was reading the values.
xdoc.Element(XName.Get("CODELIST", dns.NamespaceName)).Element(XName.Get("CODELIST_NAME", dns.NamespaceName)).Value,
Reading it this way has fixed it for me.
Upvotes: 0
Reputation: 273691
You need to specify the (default) namespace.
var xdoc = XElement.Load(fileStream);
var dns = xdoc.GetDefaultNamespace();
....
and then your loop becomes (added a few scope corrections as well)
foreach (var item in elements)
{
var codeList = new CodeList();
codeList.Name = item.Element(dns + "CODELIST_NAME").Value;
codeList.Description = item.Element(dns + "DESRIPTION").Value;
codeList.Version = item.Element(dns + "VERSION").Value;
codeList.EffectiveDate = DateTime.Parse(item.Element(dns + "EFFECTIVE_DATE").Value);
codeList.ExpirationDate = DateTime.Parse(item.Element(dns + "EXPIRATION_DATE").Value);
// save code list
}
Upvotes: 2
Reputation: 18411
In
IEnumerable<XElement> elements = xdoc.Elements();
define the parent element of the ones you are trying to parse. For instance if your xml is like:
<parent>
<CODELIST_NAME></CODELIST_NAME>
<DESRIPTION></DESRIPTION>
<VERSION></VERSION>
<EFFECTIVE_DATE></EFFECTIVE_DATE>
<EXPIRATION_DATE></EXPIRATION_DATE>
</parent>
then write it as
IEnumerable<XElement> elements = xdoc.Elements("parent");
Giannis
Upvotes: 1