{"record":{"id":"0e9d8aad976fc5f1","repo":"JeffreySu/WeiXinMPSDK","slug":"responsemessagebase-createfromresponsexml-t","errorCode":null,"errorMessage":"ResponseMessageBase.CreateFromResponseXml<T>过程发生异常！","messagePattern":"ResponseMessageBase\\.CreateFromResponseXml<T>过程发生异常！","errorType":"exception","errorClass":"WeixinException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Response/WorkResponseMessageBase.cs","lineNumber":167,"sourceCode":"\t\t\t\t\t\tresponseMessage = new ResponseMessageVideo();\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase ResponseMsgType.News:\n\t\t\t\t\t\tresponseMessage = new ResponseMessageNews();\n\t\t\t\t\t\tbreak;\n                    case ResponseMsgType.MpNews:\n                        responseMessage = new ResponseMessageMpNews();\n\t\t\t\t\t\tbreak;\n                    case ResponseMsgType.NoResponse:\n                        responseMessage = new WorkResponseMessageNoResponse();\n                        break;\n                }\n\n\t\t\t\tresponseMessage.FillEntityWithXml(doc);\n\t\t\t\treturn responseMessage;\n\t\t\t}\n\t\t\tcatch (Exception ex)\n\t\t\t{\n\t\t\t\tthrow new WeixinException(\"ResponseMessageBase.CreateFromResponseXml<T>过程发生异常！\" + ex.Message, ex);\n\t\t\t}\n\t\t}\n\t}\n}\n\n","sourceCodeStart":149,"sourceCodeEnd":173,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Response/WorkResponseMessageBase.cs#L149-L173","documentation":"CreateFromResponseXml<T> parses a response XML string into a strongly typed response entity (resolving T, filling via FillEntityWithXml). Any failure — malformed XML, XML not matching the entity schema, or T not being a valid response type — is wrapped in a WeixinException with this message plus the original exception's Message appended and InnerException chained.","triggerScenarios":"Calling CreateFromResponseXml<T>() with an XML string that is not well-formed, does not correspond to T's schema, or with an invalid T; JSON-formatted callback bodies passed where XML is expected.","commonSituations":"WeChat server returning an error JSON body (e.g. errcode JSON) instead of the expected response XML; truncation by logging proxies; SDK version where the XML schema changed.","solutions":["Log/examine ex.InnerException and the appended ex.Message to see the parse failure","Dump and validate the raw XML (e.g. XmlDocument.LoadXml) before calling the factory","Check what the upstream actually returned — a JSON error body means the earlier API call failed; handle that path instead of parsing as XML"],"exampleFix":"// before\nvar msg = WorkResponseMessageBase.CreateFromResponseXml<ResponseMessageText>(rawBody);\n// after\nif (rawBody.TrimStart().StartsWith(\"{\"))\n{\n    var err = JsonConvert.DeserializeObject<WeixinErrorJson>(rawBody);\n    throw new WeixinException($\"API error {err.errcode}: {err.errmsg}\");\n}\nvar msg = WorkResponseMessageBase.CreateFromResponseXml<ResponseMessageText>(rawBody);","handlingStrategy":"try-catch","validationCode":"try { var doc = new XmlDocument(); doc.LoadXml(xml); }\ncatch (XmlException ex) { throw new WeixinException(\"Response XML is not well-formed\", ex); }","typeGuard":null,"tryCatchPattern":"try { var msg = WorkResponseMessageBase.CreateFromResponseXml<T>(xml); }\ncatch (WeixinException ex) { log.Error($\"CreateFromResponseXml failed: {ex.Message}\", ex.InnerException); throw; }","preventionTips":["Check whether upstream returned a JSON error body before parsing as XML","Validate the XML with XmlDocument.LoadXml during development against real responses","Keep SDK versions aligned with the API schema you consume"],"tags":["xml","parse-error","exception-wrapper"],"backgroundTag":"invalid-json-response","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}