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

本文关键字:文件 xml 字符串 web 服务 响应 保存 | 更新日期: 2023-09-27 18:21:38

我正在调用一个具有.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");
        }

我怎么能那样做。

这是我们直接在brower中输入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

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

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

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

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