如何使用 linq to xml 访问 xml 文件中的图像
在我的项目中,我使用 linq 访问 xml 文件数据,xml 文件包含二进制格式的图像。
我想读取该 xml 文件并将 xml 文件中的图像分配给表单上的图片框。
但是我无法做到这一点:当我对 xml 文件应用查询时,它不理解任何元素的数据类型...
问题是什么?
这是我的示例 xml 文件 & C# 中的代码::
/////////xml file agency.xml////////////
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="agency">
<xs:complexType>
<xs:sequence>
<xs:element name="no" type="xs:int" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Slogan" type="xs:string" minOccurs="0" />
<xs:element name="Vatno." type="xs:int" minOccurs="0" />
<xs:element name="MainBranch" type="xs:string" minOccurs="0" />
<xs:element name="Disclaimer" type="xs:string" minOccurs="0" />
<xs:element name="Logo" type="xs:base64Binary" minOccurs="0" />
<xs:element name="street" type="xs:string" minOccurs="0" />
<xs:element name="town_x002F_city" type="xs:string" minOccurs="0" />
<xs:element name="State_x002F_Country" type="xs:string" minOccurs="0" />
<xs:element name="PostalCode" type="xs:int" minOccurs="0" />
<xs:element name="Phone1" type="xs:int" minOccurs="0" />
<xs:element name="Phone2" type="xs:int" minOccurs="0" />
<xs:element name="Fax" type="xs:int" minOccurs="0" />
<xs:element name="Emailid" type="xs:string" minOccurs="0" />
<xs:element name="Website" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<agency>
<no>1</no>
<Name>Good Deal Pvt</Name>
<Slogan>We Values of Your Money</Slogan>
<Vatno.>123</Vatno.>
<MainBranch>Your Branch One</MainBranch>
<Disclaimer>All the information is legal still the buyer should collect other details if needed. This intended to give information & not part of any agreement.</Disclaimer>
<Logo>here is actually that lengthy binary data for image, i have edit that </Logo>
<street>Link Raod, Near Satara Road,Pune</street>
<town_x002F_city>Pune</town_x002F_city>
<State_x002F_Country>Mahrastra</State_x002F_Country>
<PostalCode>231231</PostalCode>
<Phone1>1231313</Phone1>
<Phone2>12312323</Phone2>
<Fax>1231231</Fax>
<Emailid>[email protected]</Emailid>
<Website>www.gooddeal.com</Website>
</agency>
</NewDataSet>
////////sample code c# code to access above xml file//////////
XDocument doc = XDocument.Load("c:\\xml1\\agency.xml");
var result = from agen in doc.Descendants("agency")
where (int)agen.Element("no") == 1
select// agen;
new
{
no = (string)agen.Element("no"),
Name =(string) agen.Element("Name"),
Slogan = (string)agen.Element("Slogan"),
logo = agen.Element("Logo")
};
foreach (var tuple in result)
{
textbox1.text=tuple.Name;
textbox2.text=tuple.Slogan;
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(tuple.logo.ToString()));
Bitmap bmap = new Bitmap(ms);
this.pictureBox1.Image = bmap;
}
In my project i am accessing xml file data with linq, xml file containing images in binary format.
I want to read that xml file and assign that image in xml file to picturebox on my form.
However I am not able to do that: while i am applying queries on xml file it is not understanding datatype for any element...
What is the problem?
This is my sample xml file & code in c#::
/////////xml file agency.xml////////////
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="agency">
<xs:complexType>
<xs:sequence>
<xs:element name="no" type="xs:int" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Slogan" type="xs:string" minOccurs="0" />
<xs:element name="Vatno." type="xs:int" minOccurs="0" />
<xs:element name="MainBranch" type="xs:string" minOccurs="0" />
<xs:element name="Disclaimer" type="xs:string" minOccurs="0" />
<xs:element name="Logo" type="xs:base64Binary" minOccurs="0" />
<xs:element name="street" type="xs:string" minOccurs="0" />
<xs:element name="town_x002F_city" type="xs:string" minOccurs="0" />
<xs:element name="State_x002F_Country" type="xs:string" minOccurs="0" />
<xs:element name="PostalCode" type="xs:int" minOccurs="0" />
<xs:element name="Phone1" type="xs:int" minOccurs="0" />
<xs:element name="Phone2" type="xs:int" minOccurs="0" />
<xs:element name="Fax" type="xs:int" minOccurs="0" />
<xs:element name="Emailid" type="xs:string" minOccurs="0" />
<xs:element name="Website" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<agency>
<no>1</no>
<Name>Good Deal Pvt</Name>
<Slogan>We Values of Your Money</Slogan>
<Vatno.>123</Vatno.>
<MainBranch>Your Branch One</MainBranch>
<Disclaimer>All the information is legal still the buyer should collect other details if needed. This intended to give information & not part of any agreement.</Disclaimer>
<Logo>here is actually that lengthy binary data for image, i have edit that </Logo>
<street>Link Raod, Near Satara Road,Pune</street>
<town_x002F_city>Pune</town_x002F_city>
<State_x002F_Country>Mahrastra</State_x002F_Country>
<PostalCode>231231</PostalCode>
<Phone1>1231313</Phone1>
<Phone2>12312323</Phone2>
<Fax>1231231</Fax>
<Emailid>[email protected]</Emailid>
<Website>www.gooddeal.com</Website>
</agency>
</NewDataSet>
////////sample code c# code to access above xml file//////////
XDocument doc = XDocument.Load("c:\\xml1\\agency.xml");
var result = from agen in doc.Descendants("agency")
where (int)agen.Element("no") == 1
select// agen;
new
{
no = (string)agen.Element("no"),
Name =(string) agen.Element("Name"),
Slogan = (string)agen.Element("Slogan"),
logo = agen.Element("Logo")
};
foreach (var tuple in result)
{
textbox1.text=tuple.Name;
textbox2.text=tuple.Slogan;
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(tuple.logo.ToString()));
Bitmap bmap = new Bitmap(ms);
this.pictureBox1.Image = bmap;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当图像数据或字节数组序列化为 XML 时,它基本上是 base64 编码的数据。
您必须首先将 base64 转换为字节数组。
http://msdn.microsoft.com/en-us/library /system.convert.frombase64string.aspx
然后将该数据加载到内存流中。
然后 ms.Location = 0 (内存流起始位置)
比在位图中使用。
谢谢。
When image data or byte array serializa into XML it is basically base64 encoded data.
You have to first convert base64 to byte array.
http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx
Then load that data into memory stream.
then ms.Location = 0 ( memory stream starting location)
Than use in bitmap .
Thanks.