Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-将XML反序列化到对象时,如何跳过XML中的某些行?_C#_Xml_Deserialization - Fatal编程技术网

C#-将XML反序列化到对象时,如何跳过XML中的某些行?

C#-将XML反序列化到对象时,如何跳过XML中的某些行?,c#,xml,deserialization,C#,Xml,Deserialization,我必须用提供给我的新api端点替换旧的api端点。新api返回如下所示的xml: <?xml version="1.0" encoding="UTF-8"?> <string xmlns="https://api.com/"> <Model xmlns:msxsl="urn:schemas-microsoft-com:xslt"> <IO> <Code></Code>

我必须用提供给我的新api端点替换旧的api端点。新api返回如下所示的xml:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="https://api.com/">
    <Model xmlns:msxsl="urn:schemas-microsoft-com:xslt">
        <IO>
            <Code></Code>
            <ActyErrMsgText></ActyErrMsgText>
            <AddlDataToPrcsFlag></AddlDataToPrcsFlag>
            <PrcsPgmName></PrcsPgmName>
            <PgmLinkAreaText></PgmLinkAreaText>
        </Model>
        <Inquiry>
            <Info>
                <Mdl></Mdl>
                <Prod></Prod>
                ...and so on
我的目标是:

public static class VinTest
{
    [XmlRoot(ElementName = "Model", Namespace = "")]
    public class Model
    {
        public IO IO { get; set; }
        public Inquiry Inquiry{ get; set; }

    }
    public class IO
    {
        public string PrcsRtnStaCode { get; set; }
        public string ActyErrMsgText { get; set; }
        public string AddlDataToPrcsFlag { get; set; }
        public string PrcsPgmName { get; set; }
        public string PgmLinkAreaText { get; set; }
    }

    public class Inquiry
    {
        public Info Info { get; set; }
        public Repair RepairInfo { get; set; }
    }
    public class Info
    {
        public string Code { get; set; }
        public string Prod { get; set; }
    }
}
这是我的C代码,用于将xml反序列化到我的对象中

using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
    {
        responseText = reader.ReadToEnd();
        byte[] byteArray = Encoding.ASCII.GetBytes(responseText);
        MemoryStream stream = new MemoryStream(byteArray);

        XmlSerializer serializer = newXmlSerializer(typeof(VinTest.Model));
        var vinInfo = new VinTest.Model();
        vinInfo = (VinTest.Model)serializer.Deserialize(stream);

        return vinInfo;
    }

System.InvalidOperationException: 'There is an error in XML document (1, 40).'
InvalidOperationException: <string xmlns='https://api.com/'> was not expected.
使用(var reader=new System.IO.StreamReader(response.GetResponseStream(),编码))
{
responseText=reader.ReadToEnd();
byte[]byteArray=Encoding.ASCII.GetBytes(responseText);
MemoryStream stream=新的MemoryStream(byteArray);
XmlSerializer serializer=newXmlSerializer(typeof(VinTest.Model));
var vinInfo=new VinTest.Model();
vinInfo=(VinTest.Model)序列化程序。反序列化(流);
返回VIN信息;
}
System.InvalidOperationException:“XML文档(1,40)中存在错误。”
InvalidOperationException:不应为。
所以我遇到的问题是https://api.com/“>这是意料之中的。在将xml反序列化为对象时,如何跳过该行?

您不能非常方便地“跳过行”;您最好的选择是添加一个根类型,该根类型具有:

[XmlRoot(“字符串”,命名空间=”https://kmc--tst3.custhelp.com/")]
公共类MyRoot{
公共KMCModelExtCovPriceInquiry查询{get;set;}
}

你可以用子阅读器做你想做的,但是。。。坦率地说,这不值得付出痛苦。

顺便说一句;很小,但是
的名称空间是,IIRC,
“https://kmc--tst3.custhelp.com/“
-不是
<除非另有规定,否则将继承元素上的code>xmlns=…;名称空间前缀不可用。但是,因为这不是根元素,所以这并不重要。当XML是UTF-8时,为什么要使用ASCII编码。Ascii编码将删除不可打印的字符。所以它失去了返回字符,因为它表示以字符串开头的行在第1行,而它在第2行。