نوشته شده توسط
mahbobehshab
سلام اين سوال رو به بار ديگه مطرح كردم و لي حذفش كردند.توروخدا به من كمك كنيدخيلي ضروريه.ميخوام جدولهاي ديتابيس رو به XMLتبديل كنم وبرعكس:فايلهاي xmlرو به SQLارسال كنم.
سلام.
من نمیدونم چرا جستجو نمی کنید، من خودم حداقل یکی دو بار به چنین سوالی پاسخ داده بودم. ببینید. برای تبدیل اطلاعات هر جدولی به XML، کافیه بدین شکل عمل کنید:
SELECT * FROM Person.Contact FOR XML AUTO
با فرض به اینکه Query ی فوق رو روی بانک AdventureWorks بزنیم، مقدار جدول Person.Contact به ما بصورت XML برگردونده میشه، که بخشی از اون عبارت است از:
<Person.Contact ContactID="1" NameStyle="0" Title="Mr." FirstName="Gustavo" LastName="Achong" EmailAddress="gustavo0@adventure-works.com" EmailPromotion="2" Phone="398-555-0132" PasswordHash="GylyRwiKnyNPKbC1r4FSqA5YN9shIgsNik5A DyqStZc=" PasswordSalt="TVGHbhY=" rowguid="D4C132D3-FCB5-4231-9DD5-888A54BEC693" ModifiedDate="2005-05-16T16:33:33.060">
<AdditionalContactInfo>
<AdditionalContactInfo xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo" xmlns:crm="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord" xmlns:act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes">
<act:telephoneNumber>
<act:number>425-555-1112</act:number>
<act:SpecialInstructions>Call only after 5:00 p.m.</act:SpecialInstructions>
</act:telephoneNumber>Note that the customer has a secondary home address.<act:homePostalAddress><act:Street>123 Oak</act:Street><act:City>Seattle</act:City><act:StateProvince>WA</act:StateProvince><act:PostalCode>98001</act:PostalCode><act:CountryRegion>USA</act:CountryRegion><act:SpecialInstructions>If correspondence to the primary address fails, try this one.</act:SpecialInstructions></act:homePostalAddress>Customer provided additional email address.<act:eMail><act:eMailAddress>customer1@sam ple.com</act:eMailAddress><act:SpecialInstructions>For urgent issues, do not send e-mail. Instead use this emergency contact phone<act:telephoneNumber><act:number>425-555-1111</act:number></act:telephoneNumber>.</act:SpecialInstructions></act:eMail><crm:ContactRecord date="2001-06-02Z">This customer is interested in purchasing high-end bicycles for his family. The customer contacted Michael in sales.</crm:ContactRecord></AdditionalContactInfo>
</AdditionalContactInfo>
</Person.Contact>
<Person.Contact ContactID="2" NameStyle="0" Title="Ms." FirstName="Catherine" MiddleName="R." LastName="Abel" EmailAddress="catherine0@adventure-works.com" EmailPromotion="1" Phone="747-555-0171" PasswordHash="zh3goJUbYsPv92k4bVZuJtlLHwuvpQtu6uNc jkKSdF8=" PasswordSalt="rpyd5Tw=" rowguid="D54E0552-C226-4C22-AF3B-762CA854CDD3" ModifiedDate="2005-05-16T16:33:33.077">
<AdditionalContactInfo>
<AdditionalContactInfo xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo" xmlns:crm="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord" xmlns:act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes">These are additional phone and pager numbers for the customer.<act:telephoneNumber><act:number>206-555-2222</act:number><act:SpecialInstructions>On weekends, contact the manager at this number.</act:SpecialInstructions></act:telephoneNumber><act:telephoneNumber><act:numb er>206-555-1234</act:number></act:telephoneNumber><act:pager><act:number>206-555-1244</act:number><act:SpecialInstructions>Do not page between 9:00 a.m. and 5:00 p.m.</act:SpecialInstructions></act:pager>Customer provided this additional home address.<act:homePostalAddress><act:Street>P.O Box 5</act:Street><act:City>Edmonds</act:City><act:StateProvince>WA</act:StateProvince><act:PostalCode>98431</act:PostalCode><act:CountryRegion>USA</act:CountryRegion><act:SpecialInstructions>This is an alternative address for billing only.</act:SpecialInstructions></act:homePostalAddress><act:eMail><act:eMailAddress >Joe@sample.com</act:eMailAddress><act:SpecialInstructions>Do not send e-mail for urgent issues. Use telephone instead.</act:SpecialInstructions></act:eMail><crm:ContactRecord date="2001-07-02Z">Sales contacted this customer to explain new pricing.</crm:ContactRecord></AdditionalContactInfo>
</AdditionalContactInfo>
</Person.Contact>
همونطوریکه می بینید، به سادگی اطلاعات جدول مزبور به XML تبدیل شدش. حالا برای برگردوندن این اطلاعات به جدول (در حقیقت ذخیره اطلاعات XML در بانک)، کافیه تا با استفاده از OPENXML فیلدهای مزبور رو در Query خودم آدرس کنم و به هدفم برسم. تمام این مطالب + کلی مطلب دیگه در این خصوص رو می تونید در این مقاله مطالعه کنید.
موفق باشید.
پاورقی: طبیعتا استفاده از این روش دیگه به زبان برنامه نویسی شما ارتباطی پیدا نمیکنه، چون از تواناییهای SQLServer برای تولید و پردازش XML استفاده کرده اید.