如何将字符串 XML 转换为 C# 对象,只读取文档的内部部分。

huangapple go评论72阅读模式
英文:

How to convert string xml into C# Object reading only inner part of the document

问题

I am working on an application where I need to convert string XML into C# object but I am receiving following error.

> There is an error in XML document (1, 23)
> InvalidOperationException: <root xmlns=''> was not expected.

Here is my XML

&lt;root&gt;
  &lt;main&gt;
    &lt;url&gt;https://w3.kubotalink.com/KLWebServices/services/DealerInventorySOAP&lt;/url&gt;
    &lt;timeout&gt;80&lt;/timeout&gt;
    &lt;financialuploaddealernumber&gt;50057&lt;/financialuploaddealernumber&gt;
    &lt;ikqunitcategories&gt;2,3,4,5,6,7,8,9,10,11,12&lt;/ikqunitcategories&gt;
    &lt;ikqdebuglogenabled&gt;true&lt;/ikqdebuglogenabled&gt;
    &lt;adminuserid&gt;128&lt;/adminuserid&gt;
    &lt;locatoradminuserid&gt;120&lt;/locatoradminuserid&gt;
    &lt;warrantyenabled&gt;true&lt;/warrantyenabled&gt;
    &lt;warrantyusernameproduction&gt;integration@chartersoftware.com.ktc&lt;/warrantyusernameproduction&gt;
    &lt;warrantypasswordproduction&gt;KubotaWarranty-1&lt;/warrantypasswordproduction&gt;
    &lt;warrantyusernamestaging&gt;integration@chartersoftware.com.ktc.uat&lt;/warrantyusernamestaging&gt;
    &lt;warrantypasswordstaging&gt;Ktcwarranty1&lt;/warrantypasswordstaging&gt;
    &lt;warrantytestmode&gt;true&lt;/warrantytestmode&gt;
  &lt;/main&gt;
  &lt;locationsettings&gt;
    &lt;row&gt;
      &lt;dealerid&gt;51760&lt;/dealerid&gt;
      &lt;locationid&gt;1&lt;/locationid&gt;
      &lt;passkey&gt;kubota &lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid&gt;ikqTest1&lt;/ikquserid&gt;
      &lt;ikqpass&gt;qwerty&lt;/ikqpass&gt;
      &lt;ikqsalespersonid&gt;144&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;4&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno&gt;ABBOTT TOM&lt;/ikqcustomerno&gt;
      &lt;ikqmiscchargeid&gt;16&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename&gt;SETUP&lt;/ikqmiscchargename&gt;
      &lt;ikqunitcategoryid&gt;11&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;true&lt;/createpackingslips&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;dealerid&gt;51760&lt;/dealerid&gt;
      &lt;locationid&gt;2&lt;/locationid&gt;
      &lt;passkey&gt;family2014&lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid /&gt;
      &lt;ikqpass /&gt;
      &lt;ikqsalespersonid&gt;-2147483648&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;-2147483648&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno /&gt;
      &lt;ikqmiscchargeid&gt;-2147483648&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename /&gt;
      &lt;ikqunitcategoryid&gt;-2147483648&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;true&lt;/createpackingslips&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;dealerid&gt;51760&lt;/dealerid&gt;
      &lt;locationid&gt;3&lt;/locationid&gt;
      &lt;passkey&gt;family2014&lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid /&gt;
      &lt;ikqpass /&gt;
      &lt;ikqsalespersonid&gt;-2147483648&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;-2147483648&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno /&gt;
      &lt;ikqmiscchargeid&gt;-2147483648&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename /&gt;
      &lt;ikqunitcategoryid&gt;-2147483648&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;true&lt;/createpackingslips&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;dealerid&gt;48375&lt;/dealerid&gt;
      &lt;locationid&gt;6&lt;/locationid&gt;
      &lt;passkey&gt;charterkub1&lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid /&gt;
      &lt;ikqpass /&gt;
      &lt;ikqsalespersonid&gt;-2147483648&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;-2147483648&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno /&gt;
      &lt;ikqmiscchargeid&gt;-2147483648&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename /&gt;
      &lt;ikqunitcategoryid&gt;-2147483648&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;false&lt;/createpackingslips&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;dealerid&gt;51760&lt;/dealerid&gt;
      &lt;locationid&gt;8&lt;/locationid&gt;
      &lt;passkey&gt;family2014&lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid /&gt;
      &lt;ikqpass /&gt;
      &lt;ikqsalespersonid&gt;-2147483648&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;-2147483648&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno /&gt;
      &lt;ikqmiscchargeid
英文:

I am working on an application where I need to convert string XML into C# object but I am receiving following error.

> There is an error in XML document (1, 23)
> InvalidOperationException: <root xmlns=''> was not expected.

Here is my XML

&lt;root&gt;
  &lt;main&gt;
    &lt;url&gt;https://w3.kubotalink.com/KLWebServices/services/DealerInventorySOAP&lt;/url&gt;
    &lt;timeout&gt;80&lt;/timeout&gt;
    &lt;financialuploaddealernumber&gt;50057&lt;/financialuploaddealernumber&gt;
    &lt;ikqunitcategories&gt;2,3,4,5,6,7,8,9,10,11,12&lt;/ikqunitcategories&gt;
    &lt;ikqdebuglogenabled&gt;true&lt;/ikqdebuglogenabled&gt;
    &lt;adminuserid&gt;128&lt;/adminuserid&gt;
    &lt;locatoradminuserid&gt;120&lt;/locatoradminuserid&gt;
    &lt;warrantyenabled&gt;true&lt;/warrantyenabled&gt;
    &lt;warrantyusernameproduction&gt;integration@chartersoftware.com.ktc&lt;/warrantyusernameproduction&gt;
    &lt;warrantypasswordproduction&gt;KubotaWarranty-1&lt;/warrantypasswordproduction&gt;
    &lt;warrantyusernamestaging&gt;integration@chartersoftware.com.ktc.uat&lt;/warrantyusernamestaging&gt;
    &lt;warrantypasswordstaging&gt;Ktcwarranty1&lt;/warrantypasswordstaging&gt;
    &lt;warrantytestmode&gt;true&lt;/warrantytestmode&gt;
  &lt;/main&gt;
  &lt;locationsettings&gt;
    &lt;row&gt;
      &lt;dealerid&gt;51760&lt;/dealerid&gt;
      &lt;locationid&gt;1&lt;/locationid&gt;
      &lt;passkey&gt;kubota &lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid&gt;ikqTest1&lt;/ikquserid&gt;
      &lt;ikqpass&gt;qwerty&lt;/ikqpass&gt;
      &lt;ikqsalespersonid&gt;144&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;4&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno&gt;ABBOTT TOM&lt;/ikqcustomerno&gt;
      &lt;ikqmiscchargeid&gt;16&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename&gt;SETUP&lt;/ikqmiscchargename&gt;
      &lt;ikqunitcategoryid&gt;11&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;true&lt;/createpackingslips&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;dealerid&gt;51760&lt;/dealerid&gt;
      &lt;locationid&gt;2&lt;/locationid&gt;
      &lt;passkey&gt;family2014&lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid /&gt;
      &lt;ikqpass /&gt;
      &lt;ikqsalespersonid&gt;-2147483648&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;-2147483648&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno /&gt;
      &lt;ikqmiscchargeid&gt;-2147483648&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename /&gt;
      &lt;ikqunitcategoryid&gt;-2147483648&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;true&lt;/createpackingslips&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;dealerid&gt;51760&lt;/dealerid&gt;
      &lt;locationid&gt;3&lt;/locationid&gt;
      &lt;passkey&gt;family2014&lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid /&gt;
      &lt;ikqpass /&gt;
      &lt;ikqsalespersonid&gt;-2147483648&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;-2147483648&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno /&gt;
      &lt;ikqmiscchargeid&gt;-2147483648&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename /&gt;
      &lt;ikqunitcategoryid&gt;-2147483648&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;true&lt;/createpackingslips&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;dealerid&gt;48375&lt;/dealerid&gt;
      &lt;locationid&gt;6&lt;/locationid&gt;
      &lt;passkey&gt;charterkub1&lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid /&gt;
      &lt;ikqpass /&gt;
      &lt;ikqsalespersonid&gt;-2147483648&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;-2147483648&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno /&gt;
      &lt;ikqmiscchargeid&gt;-2147483648&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename /&gt;
      &lt;ikqunitcategoryid&gt;-2147483648&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;false&lt;/createpackingslips&gt;
    &lt;/row&gt;
    &lt;row&gt;
      &lt;dealerid&gt;51760&lt;/dealerid&gt;
      &lt;locationid&gt;8&lt;/locationid&gt;
      &lt;passkey&gt;family2014&lt;/passkey&gt;
      &lt;enablelocator&gt;false&lt;/enablelocator&gt;
      &lt;ikquserid /&gt;
      &lt;ikqpass /&gt;
      &lt;ikqsalespersonid&gt;-2147483648&lt;/ikqsalespersonid&gt;
      &lt;ikqcustomerid&gt;-2147483648&lt;/ikqcustomerid&gt;
      &lt;ikqcustomerno /&gt;
      &lt;ikqmiscchargeid&gt;-2147483648&lt;/ikqmiscchargeid&gt;
      &lt;ikqmiscchargename /&gt;
      &lt;ikqunitcategoryid&gt;-2147483648&lt;/ikqunitcategoryid&gt;
      &lt;createpackingslips&gt;true&lt;/createpackingslips&gt;
    &lt;/row&gt;
  &lt;/locationsettings&gt;
&lt;/root&gt;

Here is Model Class

[Serializable, XmlRoot(&quot;locationsettings&quot;)]
    public class LocationSettings
    {
        public LocationSettings()
        {
            this.RowSettings = new List&lt;RowSettings&gt;();
        }
        [XmlArrayItem(&quot;row&quot;)]
        public List&lt;RowSettings&gt; RowSettings { get; set; }
    }
    [Serializable, XmlRoot(&quot;row&quot;)]
    public class RowSettings
    {
        [XmlElement(&quot;dealerid&quot;)]
        public int dealerid { get; set; }
        [XmlElement(&quot;locationid&quot;)]
        public int locationid { get; set; }
        [XmlElement(&quot;ikquserid&quot;)]
        public string? ikquserid { get; set; }
        [XmlElement(&quot;ikqpass&quot;)]
        public string? ikqpass { get; set; }
    }

Here is my code item.Settings contains the above XML in string.

var xmlSettings = $&quot;&lt;?xml version=\&quot;1.0\&quot;?&gt;{item.Settings}&quot;;
    var stringReader = new System.IO.StringReader(xmlSettings);
    var serializer = new XmlSerializer(typeof(LocationSettings));
    var tt = serializer.Deserialize(stringReader) as LocationSettings;

I just need to serialize the XML into LocationSettings Class I do not want to read the other data.

答案1

得分: 1

以下是您要翻译的内容:

以下代码已经测试过。我将[XmlArrayItem]更改为[XmlElement]

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication52
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";

        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader sReader = new StringReader(xml);
            XmlReader xReader = XmlReader.Create(sReader);
            xReader.ReadToFollowing("locationsettings");
            XmlSerializer serializer = new XmlSerializer(typeof(LocationSettings));
            LocationSettings location = (LocationSettings)serializer.Deserialize(xReader);
        }
    }
    [Serializable, XmlRoot("locationsettings")]
    public class LocationSettings
    {
        public LocationSettings()
        {
            this.RowSettings = new List<RowSettings>();
        }
        [XmlElement("row")]
        public List<RowSettings> RowSettings { get; set; }
    }
    [Serializable, XmlRoot("row")]
    public class RowSettings
    {
        [XmlElement("dealerid")]
        public int dealerid { get; set; }
        [XmlElement("locationid")]
        public int locationid { get; set; }
        [XmlElement("ikquserid")]
        public string ikquserid { get; set; }
        [XmlElement("ikqpass")]
        public string ikqpass { get; set; }
    }
}

希望这对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

Following code is tested. I changed [XmlArrayItem] to [XmlElement]

<!-- begin snippet: js hide: false console: true babel: false -->

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication52
{
    class Program
    {
        const string FILENAME = @&quot;c:\temp\test.xml&quot;;

        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader sReader = new StringReader(xml);
            XmlReader xReader = XmlReader.Create(sReader);
            xReader.ReadToFollowing(&quot;locationsettings&quot;);
            XmlSerializer serializer = new XmlSerializer(typeof(LocationSettings));
            LocationSettings location = (LocationSettings)serializer.Deserialize(xReader);
        }
    }
    [Serializable, XmlRoot(&quot;locationsettings&quot;)]
    public class LocationSettings
    {
        public LocationSettings()
        {
            this.RowSettings = new List&lt;RowSettings&gt;();
        }
        [XmlElement(&quot;row&quot;)]
        public List&lt;RowSettings&gt; RowSettings { get; set; }
    }
    [Serializable, XmlRoot(&quot;row&quot;)]
    public class RowSettings
    {
        [XmlElement(&quot;dealerid&quot;)]
        public int dealerid { get; set; }
        [XmlElement(&quot;locationid&quot;)]
        public int locationid { get; set; }
        [XmlElement(&quot;ikquserid&quot;)]
        public string ikquserid { get; set; }
        [XmlElement(&quot;ikqpass&quot;)]
        public string ikqpass { get; set; }
    }

 
}

<!-- end snippet -->

答案2

得分: 1

LocationSettings模型中的[XmlArrayItem]更改为[XmlElement]即可解决问题。以下代码有效:

var xmlContent = File.ReadAllText("foo.xml");

var xml = XDocument.Parse(xmlContent);
var locationSettingsElement = xml.Root!.Element(nameof(LocationSettings).ToLowerInvariant());

var serializer = new XmlSerializer(typeof(LocationSettings));
var tt = serializer.Deserialize(locationSettingsElement!.CreateReader()) as LocationSettings;

当然,需要处理一些空值检查。

英文:

Changing [XmlArrayItem] to [XmlElement] in LocationSettings model does the trick. Following code works:

var xmlContent = File.ReadAllText(&quot;foo.xml&quot;);

var xml = XDocument.Parse(xmlContent);
var locationSettingsElement = xml.Root!.Element(nameof(LocationSettings).ToLowerInvariant());

var serializer = new XmlSerializer(typeof(LocationSettings));
var tt = serializer.Deserialize(locationSettingsElement!.CreateReader()) as LocationSettings;

Of course there are some null checks to be handled.

huangapple
  • 本文由 发表于 2023年5月25日 00:14:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定