C# String.Replace double quotes and Literals

前端 未结 4 816
孤街浪徒
孤街浪徒 2021-02-02 14:58

I\'m fairly new to c# so that\'s why I\'m asking this here.

I am consuming a web service that returns a long string of XML values. Because this is a string all the attri

4条回答
  •  野性不改
    2021-02-02 15:31

    If you are getting an XML string why not use XML instead strings?

    you will have access to all elements and attributes and it will be much easier and extremely fast if using the System.Xml namespace

    in your example you are getting this string:

    string xmlSample = "";
    

    All you need to do is convert that string into a XML Document and use it, like:

    System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
    xml.LoadXml(xmlSample);
    
    System.Xml.XmlElement _root = xml.DocumentElement;
    
    foreach (System.Xml.XmlNode _node in _root)
    {
        Literal1.Text = "
    " + _node.Name + "
    "; for (int iAtt = 0; iAtt < _node.Attributes.Count; iAtt++) Literal1.Text += _node.Attributes[iAtt].Name + " = " + _node.Attributes[iAtt].Value + "
    "; }

    in ASP.NET this will output to the Literal1 something like:

    item
    att1 = value
    att2 = value2
    

    once you have the element in a XmlElement, it is very easy to search and get the values and names for what's in that element.

    give it a try, I use it a lot when retrieving WebServices responses and when I store something in a XML file as settings for a small application for example.

提交回复
热议问题