how to ignore xml namespaces?

前端 未结 3 1982
[愿得一人]
[愿得一人] 2021-01-15 09:50

I\'ve got test xml file that looks like this:


  
   ...
  

When I\'m t

相关标签:
3条回答
  • 2021-01-15 10:35

    A namespace is fundamental in XML (unlike namespaces which are interchangeable). If Person is in that namespace, you must tell it:

    [XmlRoot(Namespace="http://tempuri.org/PaymentInformationXml.xsd")]
    public class Person {...}
    
    0 讨论(0)
  • 2021-01-15 10:38

    Check out XmlSerializerNamespaces.

    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
    ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    

    Controlling the default namespace can be done directly on the XmlSerializer:

    XmlSerializer xs = new XmlSerializer(typeof(Person), "http://tempuri.org/PaymentInformationXml.xsd");
    

    ... but your question is a little unclear about where the problem comes from.

    Check your Person classes [XmlType] attribute:

    [XmlType(Namespace="http://tempuri.org/PaymentInformationXml.xsd")]
    public class Person
    {
        //...
    }
    

    The namespace for your Person type needs to be consistent with the one you use when serializing.

    0 讨论(0)
  • 2021-01-15 10:42

    there's an article about xml here

    And i also stumbled accros this piece of code: (very helpfull)

    XmlDocument stripDocumentNamespace(XmlDocument oldDom)
    {
    // Remove all xmlns:* instances from the passed XmlDocument
    // to simplify our xpath expressions.
    XmlDocument newDom = new XmlDocument();
    newDom.LoadXml(System.Text.RegularExpressions.Regex.Replace(
    oldDom.OuterXml, @"(xmlns:?[^=]*=[""][^""]*[""])", "",
    System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline)
    );
    return newDom;
    } 
    

    hope this could help

    0 讨论(0)
提交回复
热议问题