Xml解析问题................................ [英] Xml Parsing Issue ................................
问题描述
嗨
我正在从webservice读取xml数据并将其加载到datatable中时出现错误
为不同的映射定义了列名id类型
< 国家 id = 350 标识符 = 黑山 >
< span class =code-keyword>< id > 350 < / id >
< c lient id = 0 entity-name = ADClient 标识符 = 系统 / >
< 组织 id = 0 entity-name = 组织 标识符 = * / >
< 有效 > true < < span class =code-leadattribute> / active >
< creationDate transient = true > 2013-01-30T07:35 :18.545Z < / creationDate >
< createdBy transient = true id = 0 entity-name = ADUser 标识符 = 系统 / >
< 已更新 transient = true > 2013-01-30T 07:35:18.545Z < / updated >
< updatedBy transient = true id = 0 entity-name = ADUser identifier = 系统 / >
< name > Montenegro < / name >
< description > 黑山< / description >
< iSOCountryCode 跨度> <跨度类= 代码关键字 >> 跨度> ME <跨度类= 代码关键字 >< 跨度> <跨度类= 代码leadattribute > / iSOCountryCode >
< hasRegions > false < / hasRegions &g t;
< regionName > 州< / regionName >
< phoneNoFormat xsi:nil = true / >
< ; addressPrintFormat > @ C @,@ P @ < / addressPrintFormat >
< postalCodeFormat xsi:nil = true / >
< additionalPostalCode > false < / additionalPostalCode >
< additionalPostalFormat xsi:nil = true / >
< 默认 > false < / default >
< language xsi:nil = true / >
< currency id = 102 entity-name = 货币 identifier = EUR / >
< iBANLength > 22 < / iBANLength >
< iBANCode > ME < / iBANCode >
< decimalseparator xsi:nil < span class =code-keyword> = true / >
< groupingseparator xsi:nil = true / >
< numericmask xsi:nil = true / >
< dateformat xsi:nil = true / >
< datetimeformat xsi:nil = true / >
< countryTrlList / >
< regionList / >
< / Country >
由于id同时使用作为属性和元素,这是名称ID之间的冲突..如何重命名或解决它....我不能改变webservice因为我没有访问它
用于加载数据表的代码是
Da taTable dt = new DataTable();
using(var client = new WebClient {Credentials = new NetworkCredential( 用户名, 密码)})
{
string result = client.DownloadString(uri);
XmlTextReader reader = new XmlTextReader( new StringReader(result));
dt.ReadXml(读者);
return dsGetValues;
}
谢谢
Darshan
试试这个< br $>
static void Main(string [] args)
{
DataSet ds = new DataSet();
ds.EnforceConstraints = false;
ds.ReadXmlSchema(XMLSchema1.xsd);
XmlTextReader reader = new XmlTextReader(XMLFile1.xml);
ds.ReadXml(读者);
}
我已将xml保存在名为XMLFile1.xml的文件中,并创建名为XMLSchema1.xsd的XSD 。以下是XSD的内容。希望这可以解决你的问题。
< xs:schema attributeFormDefault = 不合格 elementFormDefault = 合格 xmlns:xs = http://www.w3 .org / 2001 / XMLSchema >
< xs:element name = 国家/地区 >
< xs:complexType >
< xs:sequence >
< xs:element type = < span class =code-keyword> xs:short name = Id / >
< xs:element name =client > = id / < span class =code-keyword>>
< xs:complexType >
< xs:simpleContent >
< xs:extension base = xs:string >
< xs:attribute type = xs:short 名称 = id / >
< xs:attribute type = xs:string name = entity-name / < span class =code-keyword>>
< xs:attribute type = xs:string 名称 = 标识符 / >
< / xs:extension >
< / xs:simpleContent >
< < span class =code-leadattribute> / xs:complexType >
< / xs:element >
< xs:element name = o组织 >
< xs:complexType >
< xs:simpleContent >
< xs:extension base = xs:string >
< xs:attribute 类型 = xs:short name
< xs:attribute type = xs:string name = entity-name / >
< xs:attribute 类型 = xs:string name= 标识符 / >
< / xs:extension >
< / xs:simpleContent >
< / xs:complexType >
< / xs:element >
< xs:element type = xs:stringname = 有效 / >
< xs:element 名称 = creationDate > ;
< xs:complexType >
< xs:simpleContent >
< xs:extension base = xs:dateTime >
< xs:attribute type = xs:string 名称 = < span class =code-keyword> transient / >
< / xs:extension >
< / xs:simpleContent >
< / xs:complexType >
< / xs:element >
< xs:element name = createdBy >
< span class =code-keyword>< xs:complexType >
< xs:simpleContent >
< xs:extension base = xs:string >
< xs:attribute < span class =code-attribute> type = xs:string name = 瞬态 / >
< xs:attribute type = xs:short 名称 = id / >
< span class =code-keyword>< xs:attribute type = xs:string 名称 = 实体名称 / >
< xs:attribute 类型 = xs:string 名称 = 标识符 / >
< / xs:extension >
< / xs:simpleContent >
< / xs:complexType >
< / xs:element >
< xs:element 名称 = 已更新 >
< xs:complexType >
< xs:simpleContent >
< xs:extension base = xs:dateTime >
< xs:属性 类型 = xs:string name = tr ansient / >
< / xs:extension >
< / xs:simpleContent >
< span class =code-keyword>< / xs:complexType >
< / xs:element >
< xs:element 名称 = updatedBy >
< xs:complexType >
< xs:simpleContent >
< xs:extension base = xs:string < span class =code-keyword>>
< xs:attribute type = xs:string name = transient / >
< xs:attribute type = xs:short 名称 = id />
<xs:attribute type= \"xs:string\" name=\"entity-name\"/>
<xs:attribute type=\"xs:string\" name=\"identifier\"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type=\"xs:string\" name=\"name\"/>
<xs:element type=\"xs:string\" name =\"description\"/>
<xs:element type=\"xs:string\" name=\"iSOCountryCode\"/>
<xs:element type=\"xs:string\" name=\"hasRegions\"/>
<xs:element type=\"xs:string\" name=\"regionName\"/>
<xs:element type=\"xs:string\" name=\"phoneNoFormat\"/>
<xs:element type=\"xs:string\" name=\"addressPrintFormat\"/>
<xs:element type=\"xs:string\" name=\"postalCo deFormat\"/>
<xs:element type=\"xs:string\" name=\"additionalPostalCode\"/>
<xs:element type=\"xs:string\" name=\"additionalPo stalFormat\"/>
<xs:element type=\"xs:string\" name=\"default\"/>
<xs:element type=\"xs:string\" name=\"language\"/>
<xs:element name=\"currency\">
<xs:complexType>
<xs:simpleContent>
<xs:extension base=\"xs:string\">
<xs:attribute type=\"xs:short\" name=\"id\"/>
<xs:attribute type=\"xs:string\" name=\"entity-name\"/>
<xs:attribute type=\"xs:string\" name=\"identifier\"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element type=\"xs:short\" name=\"iBANLength\"/>
<xs:element type=\"xs:string\" name=\"iBANCode\"/>
<xs:element type=\"xs:string\" name=\"decimalseparator\"/>
<xs:element type=\"xs:string\" name=\"groupingseparator\"/>
<xs:element type=\"xs:string\" name=\"numericmask\"/>
<xs:element type=\"xs:string\" name=\"dateformat\"/>
<xs:element type=\"xs:string\" name=\"datetimeformat\"/>
<xs:element type=\"xs:string\" name=\"countryTrlList\"/>
<xs:element type=\"xs:string\" name=\"regionList\"/>
</xs:sequence>
<xs:attribute type=\"xs:short\" name=\"id\"/>
<xs:attribute type=\"xs:string\" name=\"identifier\"/>
</xs:complexType>
</xs:element>
</xs:schema>
Hi
Im reading a xml data from webservice and while loading it into datatable im getting an error
" Column name id is defined for different mapping types"
<Country id="350" identifier="Montenegro">
<id>350</id>
<client id="0" entity-name="ADClient" identifier="System"/>
<organization id="0" entity-name="Organization" identifier="*"/>
<active>true</active>
<creationDate transient="true">2013-01-30T07:35:18.545Z</creationDate>
<createdBy transient="true" id="0" entity-name="ADUser" identifier="System"/>
<updated transient="true">2013-01-30T07:35:18.545Z</updated>
<updatedBy transient="true" id="0" entity-name="ADUser" identifier="System"/>
<name>Montenegro</name>
<description>Montenegro</description>
<iSOCountryCode>ME</iSOCountryCode>
<hasRegions>false</hasRegions>
<regionName>State</regionName>
<phoneNoFormat xsi:nil="true"/>
<addressPrintFormat>@C@, @P@</addressPrintFormat>
<postalCodeFormat xsi:nil="true"/>
<additionalPostalCode>false</additionalPostalCode>
<additionalPostalFormat xsi:nil="true"/>
<default>false</default>
<language xsi:nil="true"/>
<currency id="102" entity-name="Currency" identifier="EUR"/>
<iBANLength>22</iBANLength>
<iBANCode>ME</iBANCode>
<decimalseparator xsi:nil="true"/>
<groupingseparator xsi:nil="true"/>
<numericmask xsi:nil="true"/>
<dateformat xsi:nil="true"/>
<datetimeformat xsi:nil="true"/>
<countryTrlList/>
<regionList/>
</Country>
Since id is used both as attribute and element,ther is a conflict between the name id.. How can i rename it or resolve it ....i cannot the change the webservice since i dont ve access to it
the code m using to load the datatable is
DataTable dt = new DataTable();
using (var client = new WebClient { Credentials =new NetworkCredential("Username","Password") })
{
string result = client.DownloadString(uri);
XmlTextReader reader = new XmlTextReader(new StringReader(result));
dt.ReadXml(reader);
return dsGetValues;
}
Thanks
Darshan
Try this
static void Main(string[] args) { DataSet ds = new DataSet(); ds.EnforceConstraints = false; ds.ReadXmlSchema("XMLSchema1.xsd"); XmlTextReader reader = new XmlTextReader("XMLFile1.xml"); ds.ReadXml(reader); }
I have saved your xml in file named "XMLFile1.xml" and created XSD named "XMLSchema1.xsd". Below is the content of XSD. Hoping this should solve your problem.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Country"> <xs:complexType> <xs:sequence> <xs:element type="xs:short" name="Id"/> <xs:element name="client"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:short" name="id"/> <xs:attribute type="xs:string" name="entity-name"/> <xs:attribute type="xs:string" name="identifier"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="organization"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:short" name="id"/> <xs:attribute type="xs:string" name="entity-name"/> <xs:attribute type="xs:string" name="identifier"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element type="xs:string" name="active"/> <xs:element name="creationDate"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:dateTime"> <xs:attribute type="xs:string" name="transient"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="createdBy"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:string" name="transient"/> <xs:attribute type="xs:short" name="id"/> <xs:attribute type="xs:string" name="entity-name"/> <xs:attribute type="xs:string" name="identifier"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="updated"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:dateTime"> <xs:attribute type="xs:string" name="transient"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="updatedBy"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:string" name="transient"/> <xs:attribute type="xs:short" name="id"/> <xs:attribute type="xs:string" name="entity-name"/> <xs:attribute type="xs:string" name="identifier"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element type="xs:string" name="name"/> <xs:element type="xs:string" name="description"/> <xs:element type="xs:string" name="iSOCountryCode"/> <xs:element type="xs:string" name="hasRegions"/> <xs:element type="xs:string" name="regionName"/> <xs:element type="xs:string" name="phoneNoFormat"/> <xs:element type="xs:string" name="addressPrintFormat"/> <xs:element type="xs:string" name="postalCodeFormat"/> <xs:element type="xs:string" name="additionalPostalCode"/> <xs:element type="xs:string" name="additionalPostalFormat"/> <xs:element type="xs:string" name="default"/> <xs:element type="xs:string" name="language"/> <xs:element name="currency"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:short" name="id"/> <xs:attribute type="xs:string" name="entity-name"/> <xs:attribute type="xs:string" name="identifier"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element type="xs:short" name="iBANLength"/> <xs:element type="xs:string" name="iBANCode"/> <xs:element type="xs:string" name="decimalseparator"/> <xs:element type="xs:string" name="groupingseparator"/> <xs:element type="xs:string" name="numericmask"/> <xs:element type="xs:string" name="dateformat"/> <xs:element type="xs:string" name="datetimeformat"/> <xs:element type="xs:string" name="countryTrlList"/> <xs:element type="xs:string" name="regionList"/> </xs:sequence> <xs:attribute type="xs:short" name="id"/> <xs:attribute type="xs:string" name="identifier"/> </xs:complexType> </xs:element> </xs:schema>
这篇关于Xml解析问题................................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!