0

我有这段代码可以通过 REST API 在 Azure 中配置一个实例。上次我检查它正在运行并且没有错误,但是当我今天早上重新打开并重新调试时,异常突然上升。缺少根元素。但是当我尝试将 rootElement 放入我的 xml 中时,仍然无法正常工作。

<?xml version="1.0" encoding="utf-8"?>
<rootElement>
...
</rootElement>

如果正确分配,我会检查我的 xml 的位置。似乎没有错。但是给我这个错误。这个问题的原因可能是什么

string myURL = "https://management.core.windows.net/mySubscriptionID/services/hostedservices/CloudService/deployments";
    string postBodyFileName = @"C:\Users\CompName\Desktop\REST API\VM-CreateVM.xml";
    string subscriptionID = ConfigurationManager.AppSettings["Subscription"];
    string certificatePath = ConfigurationManager.AppSettings["CertificatePath"];
    protected void Button1_Click(object sender, EventArgs e)
    {
        Uri requestUri = new Uri(myURL);

        // Create the request and specify attributes of the request.
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri);

        // Define the required headers to specify the API version and operation type.
        request.Headers.Add("x-ms-version", "2012-03-01");
        request.Method = "POST";
        request.ContentType = "application/xml";
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(postBodyFileName);

        Stream requestStream = request.GetRequestStream();
        StreamWriter streamWriter = new StreamWriter(requestStream, System.Text.UTF8Encoding.UTF8);
        xDoc.Save(streamWriter);
        streamWriter.Close();
        requestStream.Close();

        X509Certificate2 myCert = new X509Certificate2(certificatePath);
        request.ClientCertificates.Add(myCert);

        // Make the call using the web request.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        HttpStatusCode responseStatus = response.StatusCode;
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);

        XmlDocument xDocResp = new XmlDocument();
        xDocResp.Load(reader);

        string operationsResponse = xDocResp.SelectSingleNode("/*[local-name()='Operation' and namespace-uri()='http://schemas.microsoft.com/windowsazure']/*[local-name()='Status' and namespace-uri()='http://schemas.microsoft.com/windowsazure']").InnerXml;

    }
4

0 回答 0