简体   繁体   中英

C# XML Parsing - Data at the root level is invalid. Line 1, position 1

I am trying to Parse the following XML:

<connector_attribute_validation>
    <use_ssl>
       <required>1</required>
       <type>1</type>
    </use_ssl>
    <size>
        <required>1</required>
        <type>1</type>
    </size>
 </connector_attribute_validation>

My code is as follows:

 public static Dictionary<string, Dictionary<string, string>> ValidateConnectorAttributes(string xmlString)
    {
        try
        {
            Dictionary<string, Dictionary<string, string>> validation = new Dictionary<string, Dictionary<string, string>>();
            XElement root = XElement.Parse(xmlString);
            foreach (XElement attribute in root.Elements())
            {
                Dictionary<string, string> attributeInfo = new Dictionary<string, string>();
                foreach (XElement attributeInformation in attribute.Elements())
                {
                    attributeInfo.Add(attributeInformation.Name.LocalName, attributeInformation.Value);
                }
                validation.Add(attribute.Name.LocalName, attributeInfo);
            }
            foreach (KeyValuePair<string, Dictionary<string, string>> Attribute in validation)
            {
                Console.WriteLine(Attribute.Key + " : ");
                foreach (KeyValuePair<string, string> AttributeValues in Attribute.Value)
                {
                    Console.WriteLine(AttributeValues.Key + " : " + AttributeValues.Value);
                }
                Console.WriteLine("=========");
            }
            return validation;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            return null;
        }
    }

I am getting the error: Data at the root level is invalid. Line 1, position 1.

What am I doing wrong here? My goal is to convert the XML into a dictionary within a dictionary. So I'm looking for something like:

<use_ssl, <required, 1> <type, 1>>

The exact XML I am trying to parse is as follows:

<connector_attributes_validations>
<use_ssl>
        <required>0</required>
    <type>string</type> 
    <default></default>
    <valid_values>Yes|No</valid_values>
  </use_ssl>
  <page_size>
    <required>1</required>
    <type>int</type>
    <default>500</default>
    <valid_values></valid_values>
  </page_size>
  <retry_count>
    <required>1</required>
    <type>int</type>
    <default>3</default>
    <valid_values></valid_values>
  </retry_count>
  <query_timeout>
    <required>1</required>
    <type>int</type>
    <default>600</default>
    <valid_values></valid_values>
  </query_timeout>
  <domain_name>
    <required>1</required>
    <type>string</type>
    <default></default>
    <valid_values></valid_values>
  </domain_name>
  <ad_account_searchbase>
    <required>0</required>
    <type>string</type>
    <default></default>
    <valid_values></valid_values>
  </ad_account_searchbase>
  <ad_account_searchfilter>
    <required>1</required>
    <type>string</type>
    <default></default>
    <valid_values></valid_values>
  </ad_account_searchfilter>
  <ad_group_searchbase>
    <required>0</required>
    <type>string</type>
    <default></default>
    <valid_values></valid_values>
  </ad_group_searchbase>
  <ad_group_searchfilter>
    <required>0</required>
    <type>string</type>
    <default></default>
    <valid_values></valid_values>
  </ad_group_searchfilter>
</connector_attributes_validations>

I added a shortened version because both are not working for me and I do not understand why. Same error: Data at root level is invalid.

He is another method :

            XDocument doc = XDocument.Load(fFILENAME);

            Dictionary<string, Dictionary<string,string>> dict = doc.Root
                .Elements()
                .GroupBy(x => x.Name.LocalName, y => y.Elements()
                    .GroupBy(a => a.Name.LocalName, b => (string)b)
                    .ToDictionary(a => a.Key, b => b.FirstOrDefault()))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM