I'm having trouble using LINQ to XML to read my xml file. I have attached a portion of the xml schema.
<?xml version="1.0" encoding="UTF-8"?>
-<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mstns="http://tempuri.org/sdnList.xsd" xmlns="http://tempuri.org/sdnList.xsd" elementFormDefault="qualified" targetNamespace="http://tempuri.org/sdnList.xsd" id="sdnList">
-<xs:element name="sdnList">
-<xs:complexType>
-<xs:sequence>
-<xs:element name="publshInformation" maxOccurs="1">
-<xs:complexType>
-<xs:sequence>
<xs:element name="Publish_Date" maxOccurs="1" minOccurs="0" type="xs:string"/>
<xs:element name="Record_Count" maxOccurs="1" minOccurs="0" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
-<xs:element name="sdnEntry" maxOccurs="unbounded">
-<xs:complexType>
-<xs:sequence>
<xs:element name="uid" type="xs:int"/>
<xs:element name="firstName" minOccurs="0" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="title" minOccurs="0" type="xs:string"/>
<xs:element name="sdnType" type="xs:string"/>
<xs:element name="remarks" minOccurs="0" type="xs:string"/>
....CONTINUES FROM HERE
The code I'm using is as follows.
XDocument doc = XDocument.Load("c:/OFACTemp/sdn.xml");
var sdnEntry = from item in doc.Root.Descendants("sdnEntry")
select new
{
uid = item.Element("uid").Value,
firstName = item.Element("firstName").Value
};
string test = "";
foreach (var p in sdnEntry)
test = "Id: " + p.uid + " First Name: " + p.firstName;
When I break point through the code, doc loads fine and I see proper data. Doc.Root is populated but Descendants appears to have nothing. Then once down to my foreach statement the sdnEntry yields no results. This seems so simple but I can't figure out why I can't select anything. I've also tried using Elements in place of Descendants and same result. End result I need to take the xml and create C# objects.
In addition, a side question would be how would sdnEntry be handled if some sdnEntrys have for example a first name and others do not? If a first name doesn't exist for an sdnEntry then the firstName element tag doesn't even exist in the xml file. Any help would be greatly appreciated.
Here's a sample of the xml.
<?xml version="1.0" standalone="true"?>
-<sdnList xmlns="http://tempuri.org/sdnList.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<publshInformation>
<Publish_Date>05/16/2013</Publish_Date>
<Record_Count>5493</Record_Count>
</publshInformation>
-<sdnEntry>
<uid>10</uid>
<lastName>ABASTECEDORA NAVAL Y INDUSTRIAL, S.A.</lastName>
<sdnType>Entity</sdnType>
-<programList>
<program>CUBA</program>
</programList>
-<akaList>
-<aka>
<uid>4</uid>
<type>a.k.a.</type>
<category>strong</category>
<lastName>ANAINSA</lastName>
</aka>
</akaList>
-<addressList>
-<address>
<uid>7</uid>
<country>Panama</country>
</address>
</addressList>
</sdnEntry>
You need to take the default namespace into account, with LINQ to XML you do that by declaring
XNamespace df = "http://tempuri.org/sdnList.xsd";
or alternatively dynamically with
XNamespace df = doc.Root.Name.Namespace;
then you need to use the XNamespace
object to construct XName
s in your query with eg
var sdnEntry = from item in doc.Root.Descendants(df + "sdnEntry")
select new
{
uid = (string)item.Element(df + "uid"),
firstName = (string)item.Element(df + "firstName")
};
removing this part from the top line worked for me. I have no idea why its like this. but it worked. xmlns:mstns="http://tempuri.org/sdnList.xsd"
I ended up using the XmlSerializer to solve my issue. Once the xml file was downloaded via FTP I used the following code. I was then able to use C# to populate my C# objects.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader sreader = new StreamReader(responseStream);
string xmlString = sreader.ReadToEnd();
XmlSerializer serializer = new XmlSerializer(typeof(sdnList));
TextReader reader = new StringReader(xmlString);
sdnList = (sdnList)serializer.Deserialize(reader);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.