从C#中的非标准Xml文件读取数据 [英] Read data from Non Standard Xml File in c#

查看:121
本文介绍了从C#中的非标准Xml文件读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从非标准Xml文件中读取c#中的数据,例如我想读取创建者,以粗体显示< dc:creator xmlns:dc =#unknown">

I Want to read data in c# from Non Standard Xml file Such as i want to read creator Shown in bold <dc:creator xmlns:dc="#unknown">

<rdf:Seq>
  <rdf:li><b>James O'Connor</b></rdf:li> 
  <rdf:li><b>Jari-Matti Latvala & Mikko Hirvonen</b></rdf:li> 
  </rdf:Seq>
  </dc:creator>


但是我无法从此文件中读取创建者.因此,请提供此问题的解决方案.

在此先感谢

下面给出的xml文件:


But i am not able to read creator from this file.So please provide solution of this problem.

Thanks in Advance

The xml file given below:

     <?xml version="1.0" encoding="utf-8" ?> 
- <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0">
- <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
- <rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">
  <xmpMM:DocumentID>uuid:22FB0350909E4BC8 A62029D22F380EF5</xmpMM:DocumentID> 
  <xmpMM:InstanceID>uuid:2B721C4D1CA3440C A03629AD05A20C78</xmpMM:InstanceID> 
  </rdf:Description>
- <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
  <xmp:CreateDate>2011-09-09T12:17:04+01:00</xmp:CreateDate> 
  <xmp:ModifyDate>2011-09-09T12:17:04+01:00</xmp:ModifyDate> 
  <xmp:MetadataDate>2011-09-09T12:17:04+01:00</xmp:MetadataDate> 
  <xmp:CreatorTool>FotoWare w3wp</xmp:CreatorTool> 
- <xmp:XMPFileStamps>
- <rdf:Seq>
  <rdf:li>2011-09-09T12:17:04+01:00</rdf:li> 
  <rdf:li>2012-02-01T16:08:54Z</rdf:li> 
  <rdf:li>2012-02-02T05:27:08Z</rdf:li> 
  </rdf:Seq>
  </xmp:XMPFileStamps>
  </rdf:Description>
- <rdf:Description rdf:about="" xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/">
  <photoshop:Urgency>5</photoshop:Urgency> 
  <photoshop:DateCreated>2011-09-04</photoshop:DateCreated> 
  <photoshop:TransmissionReference>Ford</photoshop:TransmissionReference> 
  <photoshop:Headline>2011 Rally Australia</photoshop:Headline> 
  <photoshop:Credit>Les Kolczak</photoshop:Credit> 
  <photoshop:Source>Canon EOS-1D Mark III</photoshop:Source> 
  <photoshop:ICCProfile>Adobe RGB (1998)</photoshop:ICCProfile> 
  <photoshop:ColorMode>3</photoshop:ColorMode> 
  </rdf:Description>
- <rdf:Description rdf:about="" xmlns:fwl="http://ns.fotoware.com/iptcxmp-legacy/1.0/">
  <fwl:CreatedTime>014107+0000</fwl:CreatedTime> 
  </rdf:Description>
- <rdf:Description rdf:about="" xmlns:fwr="http://ns.fotoware.com/iptcxmp-reserved/1.0/">
  <fwr:ShortUniqueId>3888897775</fwr:ShortUniqueId> 
  <fwr:UniqueId>12F8ADC393194635 B5F7238FA9E286A5</fwr:UniqueId> 
- <fwr:History>
- <rdf:Seq>
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-08 T=06:53:09 U=15002</rdf:li> 
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-08 T=09:11:07 U=15002</rdf:li> 
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-08 T=11:21:27 U=15002</rdf:li> 
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-08 T=11:25:34 U=15002</rdf:li> 
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-08 T=15:16:17 U=15002</rdf:li> 
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-08 T=15:43:39 U=15002</rdf:li> 
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-08 T=17:08:49 U=15002</rdf:li> 
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-09 T=04:02:23 U=15002</rdf:li> 
  <rdf:li>A=5010 N=0 E=FWD D=2011-09-09 T=09:30:29 U=15002</rdf:li> 
  <rdf:li>A=5000 N=0 E=FWD D=2012-02-02 T=05:27:08 U=15002</rdf:li> 
  </rdf:Seq>
  </fwr:History>
  </rdf:Description>
- <rdf:Description rdf:about="" xmlns:tiff="http://ns.adobe.com/tiff/1.0/">
  <tiff:Make>Canon</tiff:Make> 
  <tiff:Model>Canon EOS-1D Mark III</tiff:Model> 
  <tiff:Orientation>1</tiff:Orientation> 
  <tiff:XResolution>7200000/100000</tiff:XResolution> 
  <tiff:YResolution>7200000/100000</tiff:YResolution> 
  <tiff:ResolutionUnit>2</tiff:ResolutionUnit> 
  </rdf:Description>
- <rdf:Description rdf:about="" xmlns:exif="http://ns.adobe.com/exif/1.0/">
  <exif:DateTimeOriginal>2011-09-03T23:00:00:95</exif:DateTimeOriginal> 
  <exif:DateTimeDigitized>2011-09-04T01:41:07:95</exif:DateTimeDigitized> 
  <exif:PixelXDimension>3888</exif:PixelXDimension> 
  <exif:PixelYDimension>2592</exif:PixelYDimension> 
  <exif:ExposureTime>1/640</exif:ExposureTime> 
  <exif:FNumber>4/1</exif:FNumber> 
  <exif:ExposureProgram>4</exif:ExposureProgram> 
  <exif:ExifVersion>48 50 50 49</exif:ExifVersion> 
  <exif:ShutterSpeedValue>614400/65536</exif:ShutterSpeedValue> 
  <exif:ApertureValue>262144/65536</exif:ApertureValue> 
  <exif:ExposureBiasValue>1/3</exif:ExposureBiasValue> 
  <exif:MeteringMode>5</exif:MeteringMode> 
  <exif:FocalLength>35/1</exif:FocalLength> 
  <exif:ColorSpace>2</exif:ColorSpace> 
  <exif:FocalPlaneXResolution>3888000/1107</exif:FocalPlaneXResolution> 
  <exif:FocalPlaneYResolution>2592000/736</exif:FocalPlaneYResolution> 
  <exif:FocalPlaneResolutionUnit>2</exif:FocalPlaneResolutionUnit> 
  <exif:CustomRendered>0</exif:CustomRendered> 
  <exif:ExposureMode>0</exif:ExposureMode> 
  <exif:WhiteBalance>0</exif:WhiteBalance> 
  <exif:SceneCaptureType>0</exif:SceneCaptureType> 
  <exif:GPSVersionID>2.2.0.0</exif:GPSVersionID> 
- <exif:Flash rdf:parseType="Resource">
  <exif:Fired>False</exif:Fired> 
  <exif:Return>0</exif:Return> 
  <exif:Mode>0</exif:Mode> 
  <exif:RedEyeMode>False</exif:RedEyeMode> 
  </exif:Flash>
- <exif:UserComment>
- <rdf:Alt>
  <rdf:li xml:lang="x-default" /> 
  </rdf:Alt>
  </exif:UserComment>
- <exif:ISOSpeedRatings>
- <rdf:Seq>
  <rdf:li>100</rdf:li> 
  </rdf:Seq>
  </exif:ISOSpeedRatings>
  </rdf:Description>
- <rdf:Description rdf:about="" xmlns:aux="http://ns.adobe.com/exif/1.0/aux/">
  <aux:SerialNumber>570366</aux:SerialNumber> 
  <aux:InteroperabilityIndex>R03</aux:InteroperabilityIndex> 
  </rdf:Description>
- <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
- <dc:title>
- <rdf:Alt>
  <rdf:li xml:lang="x-default">2011 WRC</rdf:li> 
  </rdf:Alt>
  </dc:title>
- <dc:subject>
- <rdf:Bag>
  <rdf:li>_AS_7377.JPG</rdf:li> 
  </rdf:Bag>
  </dc:subject>
- <b><dc:creator>
- <rdf:Seq>
  <rdf:li>James O'Connor</rdf:li> 
  <rdf:li>Jari-Matti Latvala & Mikko Hirvonen</rdf:li> 
  </rdf:Seq>
  </dc:creator></b>
- <dc:rights>
- <rdf:Alt>
  <rdf:li xml:lang="x-default">FORD</rdf:li> 
  </rdf:Alt>
  </dc:rights>
- <dc:description>
- <rdf:Alt>
  <rdf:li xml:lang="x-default">Top of the World ! Ford Abu Dhabi World Rally Team drivers, Jari-Matti Latvala and Mikko Hirvonen were joined by Aussie Rugby sensation; James O'Connor for an official pre 2011 Rally Australia photoshoot at the top of Sydney's Sky Tower</rdf:li> 
  </rdf:Alt>
  </dc:description>
  </rdf:Description>
  </rdf:RDF>
  </x:xmpmeta>

推荐答案

Useful interesting article is written by Ralf from Germany at MSDN http://msdn.microsoft.com/en-us/library/ms973822.aspx[^]
Useful interesting article is written by Ralf from Germany at MSDN http://msdn.microsoft.com/en-us/library/ms973822.aspx[^]


Hi,

As I can see this xml is not valid and I think that it can not be loaded with any XML parser. You need to escape "&" char (replace it with "&") and then you will be able to load file.

I made you a simple example using C# LinqToXml[^].
Use LINQPad[^] to run this example:
Hi,

As I can see this xml is not valid and I think that it can not be loaded with any XML parser. You need to escape "&" char (replace it with "&") and then you will be able to load file.

I made you a simple example using C# LinqToXml[^].
Use LINQPad[^] to run this example:
void Main()
{
	XNamespace dc= "http://purl.org/dc/elements/1.1/";
	XNamespace rdf = "http://www.w3.org/1999/02/22rdfsyntaxns#";

	XDocument doc = XDocument.Load(@"C:\PutYourPathHere\YourXMLFileName.xml");	
	
	var creators = from c in doc.Descendants(dc + "creator").Descendants(rdf + "li")
					select c.Value;
	
	creators.Dump("Creators");
}


As you can see there are many namespaces defined in your xml and if you want to find/select element within your xml you must write fully-qualified element name (namespace+name).


As you can see there are many namespaces defined in your xml and if you want to find/select element within your xml you must write fully-qualified element name (namespace+name).


这篇关于从C#中的非标准Xml文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆