This operation would create an incorrectly structured document

后端 未结 3 452
忘掉有多难
忘掉有多难 2020-12-18 19:31

I am new to XML and tried the following but I\'m getting an exception. Can someone help me?

The exception is This operation would create an inc

3条回答
  •  没有蜡笔的小新
    2020-12-18 20:16

    In my case I was trying to add more than one XElement to xDocument which throw this exception. Please see below for my correct code which solved my issue

    string distributorInfo = string.Empty;
    
            XDocument distributors = new XDocument();
            XElement rootElement = new XElement("Distributors");
            XElement distributor = null;
            XAttribute id = null;
    
    
            distributor = new XElement("Distributor");
            id = new XAttribute("Id", "12345678");
            distributor.Add(id);
            rootElement.Add(distributor);
    
            distributor = new XElement("Distributor");
            id = new XAttribute("Id", "22222222");
            distributor.Add(id);
            rootElement.Add(distributor);
    
            distributors.Add(rootElement);
    
    
     distributorInfo = distributors.ToString();
    

    Please see below for what I get in distributorInfo

    
     
     
    
    

提交回复
热议问题