我有这个代码通过REST API在Azure中配置一个实例。上次我检查它正在运行并没有错误,但是当我今天早上重新打开并重新调整时,Exception突然升起。缺少rootElement。但是当我试图将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;
}