C#string.IndexOf()无法正常工作

时间:2016-04-19 16:34:30

标签: c# string parsing

我正在使用XML-Like格式的文件,如:

<Tables>
    <Student>
      ID
      Roll_Number
      Name
    </Student>
</Tables>

我已经读了一个字符串中的文件,现在要进行解析,我想使用string.IndexOf(string)方法。它适用于结束标记,例如<Student><Tables>,但它为关闭标记提供了错误的索引,例如</Tables></Students>

这是我的代码:

dbFileString.Substring(dbFileString.IndexOf("<Tables>") + "<Tables>".Length, dbFileString.IndexOf("</Tables>"));

请帮忙。

2 个答案:

答案 0 :(得分:7)

这不是IndexOf您的问题,而是Substring的使用。第二个参数需要是子字符串的长度,而不是结束索引。你需要传入“结束索引 - 起始索引”作为第二个参数。

var startingIndex = dbFileString.IndexOf("<Tables>") + "<Tables>".Length
var endingIndex = dbFileString.IndexOf("</Tables>")'
var result = dbFileString.Substring(startingIndex, endingIndex - startingIndex);

但是,我真的建议您停止这种手工解析XML并使用XmlDocument之类的方法。

答案 1 :(得分:0)

dbFileString.Substring(int startIndex,int length)是正确的语法。 如果你在这里指定了你想要达到的目标,那将会很有帮助。 根据代码,我假设你试图在标签表和/表之间获取数据。 请找到下面的代码。

#!/usr/bin/env python

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gst', '1.0')
gi.require_version('GdkX11', '3.0')
from gi.repository import Gst, GObject, Gtk, GdkX11, Gdk, GstVideo

CSS_DATA = """
.snapshot_label {
background-color: rgba (50, 30, 70, 100);
background: rgba (50, 100, 10, 50);
color: white;
font-weight: bold;
font-size: 60px;
padding: 0 20px 0 20px; 
} """

class GTK_Main:
    def __init__(self):
        window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        window.set_title("Live Video Preview")
        window.set_default_size(1024, 830)
        window.connect("destroy", Gtk.main_quit, "WM destroy")
        Gtk.Window.fullscreen(window)

        style_provider = Gtk.CssProvider()
        style_provider.load_from_data(CSS_DATA)
        Gtk.StyleContext.add_provider_for_screen(
            Gdk.Screen.get_default(),
            style_provider,
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
        )

        self.overlay = Gtk.Overlay()
        window.add(self.overlay)

        self.movie_window = Gtk.DrawingArea()
        self.movie_window.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
        self.movie_window.connect("button-press-event", self.snapshot)
        self.overlay.add(self.movie_window)

        vbox = Gtk.VBox()
        hbox = Gtk.HBox()
        vbox.pack_start(hbox, False, False, 0)
        hbox.set_border_width(10)
        hbox.pack_start(Gtk.Label(), False, False, 0)
        self.button_play = Gtk.Button("Start")
        self.button_play.connect("clicked", self.start_stop)
        hbox.pack_start(self.button_play, False, False, 0)
        self.button_quit = Gtk.Button("Quit")
        self.button_quit.connect("clicked", self.exit)
        hbox.pack_start(self.button_quit, False, False, 0)

        vbox.set_valign(Gtk.Align.END)
        vbox.set_halign(Gtk.Align.CENTER)
        self.overlay.add_overlay(vbox)

        self.overlay.show_all()
        window.show_all()

        self.videoplayer = Gst.Pipeline.new("player")
        source = Gst.ElementFactory.make("videotestsrc", "file-source")
        capsfilter = Gst.ElementFactory.make("capsfilter", "capsfilter")
        caps = Gst.Caps.from_string("video/x-raw, width=1024, height=768")
        capsfilter.set_property("caps", caps)
        videosink = Gst.ElementFactory.make("xvimagesink", "video-output")

        for ele in (source, capsfilter, videosink):
            self.videoplayer.add(ele)

        source.link(capsfilter)
        capsfilter.link(videosink)

        bus = self.videoplayer.get_bus()
        bus.add_signal_watch()
        bus.enable_sync_message_emission()
        bus.connect("message", self.on_message)
        bus.connect("sync-message::element", self.on_sync_message)

    def start_stop(self, w):
        if self.button_play.get_label() == "Start":
            self.xid = self.movie_window.get_property('window').get_xid()
            self.button_play.set_label("Stop")
            self.videoplayer.set_state(Gst.State.PLAYING)
        else:
            self.videoplayer.set_state(Gst.State.NULL)
            self.button_play.set_label("Start")

    def snapshot(self, widget, event):
        if event.button == 1 and self.videoplayer.get_state(0).state == Gst.State.PLAYING:
            snapshot_label = Gtk.Label(label="SNAPSHOT!", angle=25, halign=Gtk.Align.END)
            snapshot_label.set_valign(Gtk.Align.CENTER)
            snapshot_label.set_halign(Gtk.Align.CENTER)
            snapshot_label.set_size_request(500, 300)
            snapshot_label.get_style_context().add_class("snapshot_label")
            self.overlay.add_overlay(snapshot_label)
            self.overlay.show_all()

    def exit(self, widget, data=None):
        Gtk.main_quit()

    def on_message(self, bus, message):
        t = message.type
        if t == Gst.MessageType.EOS:
            self.videoplayer.set_state(Gst.State.NULL)
            self.button_play.set_label("Start")
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print "Error: %s" % err, debug
            self.videoplayer.set_state(Gst.State.NULL)
            self.button_play.set_label("Start")

    def on_sync_message(self, bus, message):
        struct = message.get_structure()
        if not struct:
            return
        message_name = struct.get_name()
        if message_name == "prepare-window-handle":
            message.src.set_window_handle(self.xid)

Gst.init(None)
GTK_Main()
GObject.threads_init()
Gtk.main()