我在使用LINQ to XML读取我的xml文件时遇到问题。我附加了一部分xml架构。
<?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
我正在使用的代码如下。
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;
当我突破代码时,doc加载正常,我看到了正确的数据。 Doc.Root已填充,但Descendants似乎什么也没有。然后,在我的foreach语句中,sdnEntry不会产生任何结果。这看起来很简单,但我无法弄清楚为什么我不能选择任何东西。我也尝试使用Elements代替后代和相同的结果。 最终结果我需要使用xml并创建C#对象。
另外,一个侧面的问题是,如果某些sdnEntrys有一个名字而另一些没有,那么将如何处理sdnEntry?如果sdnEntry不存在名字,那么xml文件中甚至不存在firstName元素标记。 任何帮助将不胜感激。
以下是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>
答案 0 :(得分:4)
您需要考虑默认命名空间,使用LINQ to XML,通过声明
来实现XNamespace df = "http://tempuri.org/sdnList.xsd";
或者用
动态地XNamespace df = doc.Root.Name.Namespace;
然后您需要使用XNamespace
对象在您的查询中构建XName
,例如。
var sdnEntry = from item in doc.Root.Descendants(df + "sdnEntry")
select new
{
uid = (string)item.Element(df + "uid"),
firstName = (string)item.Element(df + "firstName")
};
答案 1 :(得分:0)
从顶线删除此部分对我有用。我不知道为什么会这样。但它奏效了。 的xmlns:mstns =&#34; HTTP://tempuri.org/sdnList.xsd"
答案 2 :(得分:-1)
我最终使用XmlSerializer来解决我的问题。通过FTP下载xml文件后,我使用了以下代码。然后我就可以使用C#来填充我的C#对象。
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);