0

I have a block of code that looks like this (it's from a XIB file)

<tabBar contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ZTF-8n-Y8A">
    <rect key="frame" x="2" y="431" width="320" height="49"/>
    <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
    <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
    <items>
          <tabBarItem title="Item" id="vcz-nP-1al"/>
          <tabBarItem title="Item" id="9mv-O2-GXB"/>
    </items>
</tabBar>

I have found the first line of the block by searching for the id using the following

foreach(var search in Outlets.Values)
{
    var ui = new UIObject();
    var fullSearch = string.Format("id=\"{0}\"", search);
    using (var reader = File.OpenText(infile))
    {
         var line = await reader.ReadLineAsync();
         if (line.Contains(fullSearch))

where Outlets is a Dictionary

I can grab the entire in the file being read in and store that in a string or more likely, a string builder object.

What I'm trying to do is search for key parts of the block - for example, width="320". I need to separate width="320" from the rest of the string and then remove the 320 part.

I did consider using IndexOf("width") and then count 6 along to get to the inside of the quotes up to the next quote mark, but that's probably fairly inefficient - especially if the string is long.

Is there a way to take a section of a string in the way I describe?

Nodoid
  • 1,449
  • 3
  • 24
  • 42
  • String.Substring(index, length). http://msdn.microsoft.com/en-us/library/aka44szs%28v=vs.110%29.aspx – Chubosaurus Software Sep 11 '14 at 02:40
  • 2
    You could read the contents into `XDocument` using `XDocument.Parse`. – Hassan Sep 11 '14 at 02:41
  • XIB files are XML so it is better to use XML parser instead of direct string manipulation. http://stackoverflow.com/questions/2551307/modify-xml-existing-content-in-c-sharp has some examples on how to edit XML using XDocument in C#. http://stackoverflow.com/questions/6593243/remove-attributes-from-xelement has some examples on how to delete attribute from XML element. – NickAb Sep 11 '14 at 02:44

2 Answers2

0

You can try to parse it as XML. For example, getting width value:

string str = @"<tabBar contentMode=""scaleToFill"" translatesAutoresizingMaskIntoConstraints=""NO"" id=""ZTF-8n-Y8A"">
    <rect key=""frame"" x=""2"" y=""431"" width=""320"" height=""49""/>
    <autoresizingMask key=""autoresizingMask"" widthSizable=""YES"" flexibleMinY=""YES""/>
    <color key=""backgroundColor"" white=""0.0"" alpha=""0.0"" colorSpace=""calibratedWhite""/>
    <items>
          <tabBarItem title=""Item"" id=""vcz-nP-1al""/>
          <tabBarItem title=""Item"" id=""9mv-O2-GXB""/>
    </items>
</tabBar>";

 XDocument xdoc = XDocument.Parse(str);
 string width = xdoc.Root.Element("rect").Attribute("width").Value;
IVAAAN123
  • 557
  • 2
  • 9
0

You can try this code sample.

var xml = XDocument.Load("in.xml");

if (xml.Root != null)
{
    var elements = xml.Root.XPathSelectElements("//*[@width]");
    foreach (var element in elements)
    {
        element.Attribute("width").Remove();
    }
}

xml.Save("out.xml");

First we read and parse XML file into XDocument (XDocument.Load). Then using XPath (xml.Root.PathSelectElements) we select all elements that have attribute width (that is what XPath //*[@width] stands for). After that we simply iterate over found elements and remove attribute width. This code sample will remove all width attributes, if you need to delete attribute only for a specific tag or specific value, than you should modify XPath.

If you need to delete attribute only for one specific element (e.g. for tag rect), than you can use

root.Element("rect".Attribute("width").Remove();

instead of XPath and foreach loop

NickAb
  • 6,337
  • 3
  • 15
  • 18