如何在c#中本地将响应字符串从Web服务保存到本地xml文件中

时间:2015-11-11 09:09:35

标签: c# xml web-services model-view-controller

我正在调用一个扩展名为.ashx并将字符串作为响应的Web服务。现在我正在尝试将其保存在xml文件中。项目正确执行但不保存在xml文件中。

 public void savexmlFile()
        {
            string wbserviceUrl = "https://someurl.ashx";
            WebClient clientOne = new WebClient();
            string result = clientOne.DownloadString(wbserviceUrl);

            XmlDocument cruisexmlDocument = new XmlDocument();
            cruisexmlDocument.LoadXml(result);
            cruisexmlDocument.Save("D:/brian office projects/Cruise/Cruise/XmlFiles/Cruisedata/cruiseproduts.xml");
        }

我该怎么做。

这是我们直接在浏览器中输入url的结果。

<CruiseData CreationDate="2015-11-11T00:00:03.9702000+00:00">
<CruiseProduct>
<ID>3706</ID>
<Name>MS SUNRISE SERMIRAMIS COLLECTION</Name>
<Description>
<p>Our Selected Nile Cruise <b>MS Sunrise Semiramis</b> <b>Collection Nile Cruise</b> the world’s greatest open air museum, to Aswan. Be enchanted by the fascinating landscape and rich cultural heritage. On our cruiser you will surely experience one of your most memorable vacations.<br><br><b>The Nile is the world's longest river</b> and a luxury cruise on an elegant ship is the most relaxing way to discover the cultural landmarks and archaeological sites of Egypt. Retrace the routes followed by Egypt's pharaohs from Luxor and wonder at the views over Lake Nasser from Aswan's High Dam.<br><b><br>We offer a choice of luxury Nile cruises</b> with various itineraries along the Nile and across Lake Nasser, and are delighted to feature a small selection of the most intimate, elegant and sophisticated vessels. Normal cruise itineraries include embarkation at Luxor, a cruise to Aswan, then disembarkation in Luxor, although some cruises end in Aswan, or can be booked from Aswan. For cruises ending or starting in Aswan, we can easily book travel between Aswan and Luxor.<br><br><b>Nile cruise holidays are an exceptional</b> way to visit some of Egypt’s most famed and most captivating sights. The famed Valley of the Kings contains more than sixty tombs, chambers and halls: the most famous, Tutankhamen, contains a tomb with the mummy in situ, and the Valley of the Queens offers interesting insights into the Egyptian way of life as Queens and royal children were buried in separate valleys. One of the most magnificent monuments is the Temple of Queen Hatshepsut: dedicated to Egypt’s greatest female pharaoh. The Colossi of Memnon which is two huge stone sentinels overlooking the Nile, requires special mention, as does the Temple of Luxor, and the Temple of Karnack with its daily Sound and Light Show.<br><br><b>With Combo Holidays River Nile cruises</b> covering the routes between Luxor and Aswan; the cruise itineraries feature some fantastic stops along the way. At Edfu there’s a very well preserved temple dedicated to Horus - the second largest temple in Egypt. The Aswan High Dam built during the 1960’s to stem annual floods and provide hydroelectricity, boasts good views over Lake Nasser, and the two islands of the Nile offer contrasting attractions; Kitchener's Island boasts the exotic Botanical Gardens and Elephantine Island is home to the Temple of Khnum with its ram mummies on view at the museum.<br><br><b>MS Sunrise Semiramis</b> will take you on a cruise from Luxor, the world’s greatest open air museum, to Aswan. Be enchanted by the fascinating landscape and rich cultural heritage. On our cruiser you will surely experience one of your most memorable vacations.<br><br><b>64 Standard Cabins</b> are waiting to welcome you. All cabins offer an amazing view on the Nile. The finest facilities can be

2 个答案:

答案 0 :(得分:0)

字符串,你从服务下载的是xml格式吗?如果没有,cruisexmlDocument.LoadXml(result);没有生效,它只是在内存中加载XML字符串。

答案 1 :(得分:0)

只需将您的内容下载为字符串,然后编写即可。您不需要以XML格式处理它,除非您需要以某种方式验证它是否是有效的XML。除此之外,只需下载并编写即可。确保您有权写入您的路径。

        var httpClient = new HttpClient();
        var result = httpClient.GetAsync("your url").Result;
        System.IO.File.WriteAllText(@"C:\yourxml.xml", result.Content.ReadAsStringAsync().Result);