【问题标题】:how to replace xsi:type inside a node in XML? [duplicate]如何在 XML 中的节点内替换 xsi:type? [复制]
【发布时间】:2020-06-14 22:39:07
【问题描述】:

我有一个 XML 输出,但反序列化存在问题,例如:MemoItem3 xsi:type="xsd:string"/> 我需要删除或替换 xsi:type="xsd:string" 为空,就像MemoItem3 />

注意我试过了,但没有用:

  if (!string.IsNullOrEmpty(IO_StrOut) && IO_StrOut.Contains("xsi:type"))
  {

      Regex.Replace(IO_StrOut, @"\s+xsi:type=""\w+""", "");
  };

这里的 XML 我需要替换任何 xsi:type

<Header>
      <MemoList>
        <MemoItem1 />
        <MemoItem2 />
        <MemoItem3 xsi:type="xsd:string" />
        <MemoItem4 xsi:type="xsd:string">11.1</MemoItem4>
        <MemoItem5 xsi:type="xsd:string">5.0</MemoItem5>
        <MemoItem7 xsi:type="xsd:string">AndroidPhone</MemoItem7>
      </MemoList>
      <User>
        <UserID />
      </User>
      <Service>
        <ServiceID xsi:type="xsd:string">ContactInquiry</ServiceID>
        <ServiceMessageType xsi:type="xsd:string">ContactInquiry</ServiceMessageType>
        <ServiceRequestID xsi:type="xsd:string"></ServiceRequestID>
        <ServiceRequestLanguageCode>EN</ServiceRequestLanguageCode>
        <ServiceResult>
          <ResultCode>0</ResultCode>
          <ResultDesc />
        </ServiceResult>
        <ServiceRequestTime>2020-03-02T18:46:22.163+03:00</ServiceRequestTime>
      </Service>
      <CachingAndExpiryControl />
    </Header>

【问题讨论】:

  • 你在反序列化过程中遇到了什么问题
  • System.InvalidOperationException: XML 文档中存在错误 (32, 6)。 ---> System.InvalidOperationException:未定义命名空间前缀“xsd”。
  • 好的。你能在帖子中添加xml和模型吗
  • XML 无效,然后...包裹在&lt;root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; 中并且应该可以工作
  • 如果您遇到问题,请联系生成文件的人员。

标签: c# regex xml replace deserialization


【解决方案1】:

我不知道反序列化,但 the regex seems wrong 因为 ':' 不是与“\w”匹配的“单词”。

也许this 可以匹配?

【讨论】:

    猜你喜欢
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多